Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular DI: Injecting a value token into factory provider

Is it possible to inject an InjectionToken into a factory provider:

Currently, I've coded that:

export const HOST_TOKEN = new InjectionToken<string>("host");

let configDataServiceFactory = (userService: UserService, host: string) => {
    return new Config("host", 8080, userService.getUser(), userService.getPasswd());
};

export let configDataServiceProvider =
{
    provide: CONFIG_API,
    useFactory: configDataServiceFactory,
    deps: [UserService, HOST_TOKEN]
};

Into my module:

@NgModule(
  providers: [
    {provide: HOST_TOKEN, useValue: "allianz-host"},
    configDataServiceProvider
  ]
)

Nevertheless, angular is injecting on configDataServiceProvider value "host", instead of "host-allianz"

Any ideas?

like image 236
Jordi Avatar asked Jun 07 '18 13:06

Jordi


People also ask

What does @inject do in Angular?

@Inject() is a manual mechanism for letting Angular know that a parameter must be injected. Injecting ChatWidget component to make the component behave like a singleton service so that the component state remain same across the app.

What is the default token in Angular dependency injector?

If you specify the service class as the provider token, the default behavior is for the injector to instantiate that class using the new operator. In the following example, the Logger class provides a Logger instance.

How do you inject service in Angular?

Angular provides the ability for you to inject a service into a component to give that component access to the service. The @Injectable() decorator defines a class as a service in Angular and allows Angular to inject it into a component as a dependency.

What is DI providers in Angular?

Dependency Injection (DI) is a design pattern that creates the dependencies of a class and provides those objects to the class when required.


1 Answers

Yes, you can do that using @Inject decorator like below, which will help you to extract the relevant dependency from DI container.

let configDataServiceFactory = (userService: UserService, @Inject(HOST_TOKEN) host: string) => {
    return new Config(host, 8080, userService.getUser(), userService.getPasswd());
};

You can also consider below option as well. Basically all registered InjectionToken's would be available to retrieve from application Injector, that can be achieve by calling get method over injector instance and pass InjectorToken name.

export const HOST_TOKEN = new InjectionToken<string>("host");

let configDataServiceFactory = (userService: UserService, injector: Injector) => {
    let host = injector.get(HOST_TOKEN); //retrieved token from injector
    return new Config(host, 8080, userService.getUser(), userService.getPasswd());
};

export let configDataServiceProvider =
{
    provide: CONFIG_API,
    useFactory: configDataServiceFactory,
    deps: [UserService, Injector]
};

Docs Here

like image 156
Pankaj Parkar Avatar answered Nov 15 '22 03:11

Pankaj Parkar