Questions tagged [angular-di]

34 questions
71
votes
1 answer

"this.appInits[i]" is not a function

I am trying to use APP_INITIALIZER TO load data from config file. I'm getting following error: "Unhandled Promise rejection: this.appInits[i] is not a function ; Zone: ; Task: Promise.then ; Value: TypeError: this.appInits[i] is not a…
Rakesh
  • 803
  • 1
  • 6
  • 11
33
votes
7 answers

Angular (v5) service is getting constructed before APP_INITIALIZER promise resolves

I'm expecting Angular to wait until my loadConfig() function resolves before constructing other services, but it is not. app.module.ts export function initializeConfig(config: AppConfig){ return () => config.loadConfig(); } @NgModule({ …
28
votes
3 answers

useClass vs useExisting

When should we use useExisting provider instead of useClass? providers: [ {provide: Class1, useClass: Class1}, {provide: Class2, useExisting: Class2}] REMARK: I have not found an exact question on SO. And for better indexing decided to create…
Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176
22
votes
4 answers

How to update dependency injection token value

Angular dependency injection let you inject a string, function, or object using a token instead of a service class. I declare it in my module like this: providers: [{ provide: MyValueToken, useValue: 'my title value'}] and I use it like…
8
votes
1 answer

Is using @Injectable providedIn for any non-lazy-loaded module the same as providedIn: "root"?

I've read the docs thoroughly and, although I can't find exactly where it's referenced, I have the impression that declaring a service in an eagerly loaded module's providers array will make the singleton available to the application scope. If this…
papiro
  • 2,158
  • 1
  • 20
  • 29
6
votes
1 answer

Why doesn't Angular CLI add services in providers array automatically?

As when we create Component, Pipe with angular-cli commands, CLI automatically adds them in declaration array of a specific module, why this doesn't happens in the case of services.
5
votes
1 answer

StaticInjector vs ReflectiveInjector in Angular

Angular 5.x will include new StaticInjector as mentioned in this tweet. I have two questions: How is it different from existing ReflectiveInjector? Will it break any of my existing code?
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
4
votes
1 answer

NG0200: Circular dependency in DI detected for ApplicationRef

I got the following error while refactoring the app: main.ts:21 Error: NG0200: Circular dependency in DI detected for ApplicationRef. Find more at https://angular.io/errors/NG0200 at throwCyclicDependencyError (core.js:216) at…
Daniel Kucal
  • 8,684
  • 6
  • 39
  • 64
3
votes
1 answer

Angular dynamic DI with string token

Seems that Angular 6 (4+?) requires token objects to be unique in order for the DI to work. Yet I want to use a dynamic token, to be provided from template html code, which will allow my new directive to resolve a service by name. Currently the…
rubmz
  • 1,947
  • 5
  • 27
  • 49
3
votes
1 answer

Component Inheritance and DI for base class resolver

I have a different types of components but sharing some common algorithms. So I have decided to use Component Inheritance in Angular and tried with template method pattern. Basically, I need to do a different implementation for some of the…
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
3
votes
1 answer

Angular 4 DI : wait for method completion

To avoid cyclic dependency i am using the Injector to inject the AuthService but when i run the application Angular execute the intercept() method before setting the authService property !!! @Injectable() export class TokenInterceptorService…
SEY_91
  • 1,615
  • 15
  • 26
2
votes
1 answer

How to add another provider to the injector?

A framework-agnostic way of phrasing this question is "How to register another service with the service locator?" The Injector is set up to be immutable, both the interface and the implementation. interface Injector { abstract get(token: any,…
Erik van Velzen
  • 6,211
  • 3
  • 23
  • 23
2
votes
1 answer

How to pass dependencies to @auth0-angular-jwt?

So... I'm migrating my "old" code that was using the HttpModule and angular2-jwt lib. Before, I could make angular2-jwt work with the following config: export function authHttpServiceFactory( http: Http, options: RequestOptions, myService:…
dev_054
  • 3,448
  • 8
  • 29
  • 56
2
votes
2 answers

How to get QueryParam in factory provider (Angular4)

I want to provide a constant in my application basing on QueryParam from url. localhost:4200?flag=true In my module providers I added { provide: FLAG, useFactory: FlagFactory, deps: [...] } So I am interested if there is a way how to do it…
mgrinko
  • 515
  • 1
  • 4
  • 13
2
votes
1 answer

How to keep only one instance of service for several components?

I use DI to inject sevice in several components. Now it works not as one common instance. How to keep only one instance of service for several components? I trie to load service in app.module in section providers
POV
  • 11,293
  • 34
  • 107
  • 201
1
2 3