Dependency Injection in Angular/ Calling a method that belongs to a service from a class

Paul

I have a component that creates an object (of type TableDataSource) and in the constructor I am also passing in a method that returns a promise (save method), however when the method gets called by TableDataSource, the service the method belongs to is not defined.

How would I go about injecting the service into TableDataSource given that it has to be generic. In this case I need a method that belongs to ContactService, but in another component I would need UserService for instance.

@Component({
  selector: 'app-contact-details',
})
export class ContactDetailsComponent {
  constructor(
    private contactService: ContactService,
  ) 
  {
    this.dataSource = new TableDataSource<IContactDetail>(this.save);
  }

  public save(item: IContactDetail): Promise<IContactDetail[]> {
    return this.contactService.save(item); <--- this.contactService is null
  }
}

This is the TableDataSource:

export class TableDataSource<T> extends DataSource<TessTableRow<T>> {

  protected save: (data: T) => Promise<T | T[]>;

  constructor(
    save: (data: T) => Promise<T | T[]>
  ) {
    super();

    this.save = save;
    }
}
v.kostenko

In this case you need to bind the context to save method because when you assign the method in another variable and call this method through that variable you have lost the original context:

this.dataSource = new TableDataSource<IContactDetail>(this.save.bind(this));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling service method from another service class

From

Calling activity class method from Service class

From Dev

Dependency injection from one class to method of another class

From Dev

Angular Service dependency injection on same component from different routes

From Dev

Spring dependency injection generic service class

From Dev

Using dependency injection how can I access a method in a class inside a service?

From Dev

Mock method on the same class with dependency Injection - PHP

From Dev

Dependency injection to class-method in ABAP

From Dev

dependency injection in Angular when extending a class

From Dev

Dependency injection in abstract class with TypeScript and Angular 5

From Dev

Angular2 dependency injection abstract class

From Dev

SpringBoot @Autowired NullPointerException when calling method from service class

From Dev

Calling a method from a class

From Dev

Dependency Injection from an Abstract T type class

From Java

Dependency Injection of Singleton Class that extends from Interface

From Dev

Symfony setup dependency injection from extended class

From Dev

PHP Dependency injection and extending from an umbrella class

From Dev

Angular - Calling method on service from component returns undefined

From Dev

Logger dependency injection, getting calling class name and source file path

From Dev

How to add Definiton of Symfony Dependency injection container builder from Class factory method?

From Dev

How to run non static method from static inside single class and use dependency injection C#

From Dev

Swift dependency injection by passing class as a function parameter to call a class method

From Dev

Dependency Injection: No Service for type

From Dev

Dependency Injection Into Service

From

Calling method from a Angular 2 class inside template

From Dev

Symfony 4 Service Dependency Injection - Constructor vs Method

From Java

Calling one GrpcService from other using dependency injection

From Dev

Calling HomeController action with Dependency injection from Razor Page Model

From Dev

Unit test: Mock service class with constructor dependency injection

Related Related

  1. 1

    Calling service method from another service class

  2. 2

    Calling activity class method from Service class

  3. 3

    Dependency injection from one class to method of another class

  4. 4

    Angular Service dependency injection on same component from different routes

  5. 5

    Spring dependency injection generic service class

  6. 6

    Using dependency injection how can I access a method in a class inside a service?

  7. 7

    Mock method on the same class with dependency Injection - PHP

  8. 8

    Dependency injection to class-method in ABAP

  9. 9

    dependency injection in Angular when extending a class

  10. 10

    Dependency injection in abstract class with TypeScript and Angular 5

  11. 11

    Angular2 dependency injection abstract class

  12. 12

    SpringBoot @Autowired NullPointerException when calling method from service class

  13. 13

    Calling a method from a class

  14. 14

    Dependency Injection from an Abstract T type class

  15. 15

    Dependency Injection of Singleton Class that extends from Interface

  16. 16

    Symfony setup dependency injection from extended class

  17. 17

    PHP Dependency injection and extending from an umbrella class

  18. 18

    Angular - Calling method on service from component returns undefined

  19. 19

    Logger dependency injection, getting calling class name and source file path

  20. 20

    How to add Definiton of Symfony Dependency injection container builder from Class factory method?

  21. 21

    How to run non static method from static inside single class and use dependency injection C#

  22. 22

    Swift dependency injection by passing class as a function parameter to call a class method

  23. 23

    Dependency Injection: No Service for type

  24. 24

    Dependency Injection Into Service

  25. 25

    Calling method from a Angular 2 class inside template

  26. 26

    Symfony 4 Service Dependency Injection - Constructor vs Method

  27. 27

    Calling one GrpcService from other using dependency injection

  28. 28

    Calling HomeController action with Dependency injection from Razor Page Model

  29. 29

    Unit test: Mock service class with constructor dependency injection

HotTag

Archive