Inject service into class (not component) Angular2

Corada

I am struggling to find a way to inject a service into an class object in angular2.

* NOTE: This is not a component, just a class. *

export class Product {

  id: number;
  name: string;
  manufacturer: string;

  constructor(product: any) {

    this.id = product.id;
    this.name = product.name;
    this.manufacturer = product.manufacturer;

}

The only solution I have come up with is to pass the service reference to the constructor whenever I create a new product... ie: instead of new Product(product) I would do new Product(product, productService) . This seems tedious and error prone. I would rather import the reference from the class and not messy up the constructor.

I have tried the ReflectiveInjector:

let injector = ReflectiveInjector.resolveAndCreate([ProductService]);
this.productService = injector.get(ProductService);

However, this creates an error No provider for Http! (ProductService -> Http) at NoProviderError.BaseError [as constructor] (Also I'm pretty sure this creates a new productService when I simple want to reference my singleton that is instantiated at the app level).

If anyone knows of a working solution I would be glad to hear it. For now i will pass the reference through the constructor.

Thanks

Günter Zöchbauer

There is no way to inject a service into a plain class. Angular DI only injects into components, directives, services, and pipes - only classes where DI creates the instance, because this is when injection happens.

To get Http from a custom injector, you need to add to it's providers like shown in Inject Http manually in angular 2

or you pass a parent injector that provides them

// constructor of a class instantiated by Angulars DI
constructor(parentInjector:Injector){
  let injector = ReflectiveInjector.resolveAndCreate([ProductService]);
  this.productService = injector.get(ProductService, parentInjector);
}

See also https://angular.io/docs/ts/latest/api/core/index/ReflectiveInjector-class.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

How to inject service into class (not component)

From Dev

Inject service into angular 1.5 component

From Dev

Inject a service in a model class Angular

From Dev

Angular 2 inject service into extended class(BaseRequestOptions)

From Dev

Angular 2 inject service to Utilities class

From Dev

No provider for service error in angular2, why do I need to inject it in it's parent component?

From Dev

Angular2 inject a service from AppModule into dependent NgModule's component at runtime?

From Dev

Way to inject angular FormBuilder service to dynamic component

From Dev

How To Inject Service Into Angular 1.5 Component (No Typescript)

From Dev

How to Inject Angular2 Http service into es6/7 Class?

From Dev

Angular2 inject parent service into child

From Dev

Inject a service in a custom validator in Angular2

From Dev

How inject service in component

From Dev

Inject the instance of a component in a service

From Dev

How to inject a service component in Spring Test class using Kotlin?

From Dev

When to inject a SERVICE to Angular 2 Module level and App Component Level

From Dev

Angular library module inject service with abstract class

From Dev

Angular,Not able to inject service in a parent class

From Dev

Inject dependency service to parent class in angular

From Dev

How to inject class into Service [Angular 6]

From Dev

Angular2: Inject a non @Injectable class

From Dev

Angular2 Inject service in another service creates 2 instances

From Dev

Angular: How do I inject a service into a component using appInject?

From Dev

How do I inject an AngularJS service in an Angular 5 component?

From Dev

Why won't my Service inject properly into a component in Angular?

From Dev

spyOn service / component Angular2

From Dev

Angular2 call a Component from a Service

From Dev

Angular2 how to inject parent component into directive only if it's there?

From Dev

What is the difference in Angular2 between inject a provider in @Component and @Module?

Related Related

  1. 1

    How to inject service into class (not component)

  2. 2

    Inject service into angular 1.5 component

  3. 3

    Inject a service in a model class Angular

  4. 4

    Angular 2 inject service into extended class(BaseRequestOptions)

  5. 5

    Angular 2 inject service to Utilities class

  6. 6

    No provider for service error in angular2, why do I need to inject it in it's parent component?

  7. 7

    Angular2 inject a service from AppModule into dependent NgModule's component at runtime?

  8. 8

    Way to inject angular FormBuilder service to dynamic component

  9. 9

    How To Inject Service Into Angular 1.5 Component (No Typescript)

  10. 10

    How to Inject Angular2 Http service into es6/7 Class?

  11. 11

    Angular2 inject parent service into child

  12. 12

    Inject a service in a custom validator in Angular2

  13. 13

    How inject service in component

  14. 14

    Inject the instance of a component in a service

  15. 15

    How to inject a service component in Spring Test class using Kotlin?

  16. 16

    When to inject a SERVICE to Angular 2 Module level and App Component Level

  17. 17

    Angular library module inject service with abstract class

  18. 18

    Angular,Not able to inject service in a parent class

  19. 19

    Inject dependency service to parent class in angular

  20. 20

    How to inject class into Service [Angular 6]

  21. 21

    Angular2: Inject a non @Injectable class

  22. 22

    Angular2 Inject service in another service creates 2 instances

  23. 23

    Angular: How do I inject a service into a component using appInject?

  24. 24

    How do I inject an AngularJS service in an Angular 5 component?

  25. 25

    Why won't my Service inject properly into a component in Angular?

  26. 26

    spyOn service / component Angular2

  27. 27

    Angular2 call a Component from a Service

  28. 28

    Angular2 how to inject parent component into directive only if it's there?

  29. 29

    What is the difference in Angular2 between inject a provider in @Component and @Module?

HotTag

Archive