Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection in Angular 2 when a constructor has arguments

I have a typescript class representing a model and I would like instances to communicate with an API via angular's Http service.

But the constructor of the model needs arguments when creating instances. For example something super simple:

class SomeModel{
    constructor(public id:number, public name:string, ){
    }

I would like to inject the Http service so it is available to my instances, but it seems like the canonical way to do this commandeers the constructor with:

constructor(http:Http)

I've been digging through the Injector docs, but it's a little sparse and I haven't found anything that works. Is there a way to get a reference to a service like Http from the DI system without using the constructor pattern?

like image 859
Mark Avatar asked Jan 21 '16 23:01

Mark


People also ask

How do I inject a dependency in an angular component?

You can tell Angular to inject a dependency in a component's constructor by specifying a constructor parameter with the dependency type. Here's the AppComponent constructor, asking for the HttpClient to be injected: When a component or service needs a dependency, the class constructor takes that dependency as a parameter.

What is injector class in angular?

Injector Class - Injects the service class object into the client class. 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.

How do you inject dependencies into a class?

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. Interface injection: The dependency provides an injector method that will inject the dependency into any client passed to it.

What is the use of injectable in angular?

The @ Injectable () decorator marks it as a service that can be injected, but Angular can't actually inject it anywhere until you configure an Angular dependency injector with a provider of that service. The injector is responsible for creating service instances and injecting them into classes like HeroListComponent .


1 Answers

I managed to solve the same problem using angular 4. First you create new injector that uses component injector. It knows about your SomeModel class and passes modelParams as instance of SomeModelParameters class. Then you use this newly created injector to create class instance.

@Injectable()
class SomeModel {
    constructor(http: Http, someModelParamters: SomeModelParameters) { }
}

export class MyComponent {
    constructor(injector: Injector) {
        const modelParams = new SomeModelParameters();
        const injectorWithModelParams = ReflectiveInjector.resolveAndCreate(
            [
                SomeModel,
                { provide: SomeModelParameters, useValue: modelParams }
            ],
            injector);
        this.someModel = injectorWithModelParams.resolveAndInstantiate([SomeModel]);
    }
}
like image 52
Stanislav Berkov Avatar answered Sep 30 '22 15:09

Stanislav Berkov