Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject service into class (not component) Angular2

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

like image 312
Corada Avatar asked Mar 19 '17 20:03

Corada


People also ask

How a service can be injected to a component class?

The @Injectable() decorator defines a class as a service in Angular and allows Angular to inject it into a component as a dependency. Likewise, the @Injectable() decorator indicates that a component, class, pipe, or NgModule has a dependency on a service. The injector is the main mechanism.

How do you inject a Modelling service?

let sell = new Sell(this.userService.id); Second way is to create another SellService, which will have UserService injected. It will have method createNewSell(), which will be the same as code snippet above.

How many ways we can inject service in Angular?

There are three types of Dependency Injections in Angular, they are as follows: Constructor injection: Here, it provides the dependencies through a class constructor. Setter injection: The client uses a setter method into which the injector injects the dependency.

What is @inject Angular?

@Inject() is a manual mechanism for letting Angular know that a parameter must be injected. It can be used like so: import { Component, Inject } from '@angular/core'; import { ChatWidget } from '../components/chat-widget'; ​


1 Answers

I was struggling with a similar issue, and what I ended up doing, was making the service a singleton as well as an Angular injectable.

This way you can inject via DI into Angular classes and call the static getInstance() method to get the singleton instance of the class.

Something like this:

import {Injectable} from "@angular/core";
@Injectable()
export class MyService {

  static instance: MyService;

  static getInstance() {
    if (MyService.instance) {
      return MyService.instance;
    }

    MyService.instance = new MyService();
    return MyService.instance;
  }

  constructor() {
    if (!MyService.instance) {
      MyService.instance = this;
    }
    return MyService.instance;
  }
}
like image 128
Karvapallo Avatar answered Oct 04 '22 18:10

Karvapallo