Victor Mukherjee
Victor Mukherjee

Reputation: 11065

Angular: Provide same instance for multiple tokens

I want to do something like this:

{
  provide: [Token1, Token2],
  useClass: MyService,
},

Something which I would do with Autofac in .Net like containerBuilder.Register<MyService>().As<IService1>().As<IService2>(). The instance of the object requested via both tokens should be the same. Is it possible with angular?

Upvotes: 5

Views: 1292

Answers (2)

Partha
Partha

Reputation: 106

Try this one.

{ provide: Token1, useClass: MyService} { provide: Token2, useExisting: MyService}

https://angular.io/guide/dependency-injection-providers

Upvotes: 8

Leandro Lima
Leandro Lima

Reputation: 1164

To provide the same instance you can use useExisting

[
    { provide: Token1, useClass: MyService},
    { provide: Token2, useExisting: MyService}
]

https://angular.io/api/core/ExistingProvider

Upvotes: 3

Related Questions