Questions tagged [angular-dependency-injection]

Use this tag if your question is specially about the behavior of the angular dependency injection framework.

Dependency injection (DI), is an important application design pattern. Angular has its own DI framework, which is typically used in the design of Angular applications to increase their efficiency and modularity.

Dependencies are services or objects that a class needs to perform its function. DI is a coding pattern in which a class asks for dependencies from external sources rather than creating them itself.

In Angular, the DI framework provides declared dependencies to a class when that class is instantiated. This guide explains how DI works in Angular, and how you use it to make your apps flexible, efficient, and robust, as well as testable and maintainable.

For more information about angular DI visit the official documentation

75 questions
2
votes
1 answer

How to provide an `InjectionToken` that has its own `factory`?

Consider the following InjectionToken for the type Foo: export const FOO = new InjectionToken( 'foo token', { factory: () => new Foo() }); Now assume I was crazy enough to aim for 100% test coverage. To that end I'd have to unit test that…
Good Night Nerd Pride
  • 6,287
  • 3
  • 38
  • 54
2
votes
3 answers

When does a service get created by dependency injection

I have an angular service containing some init code in its constructor. When is this code called, i.e. when is the service created? Already when the service is provided in some module? Or not until the first component injects it?
Tho.Tra
  • 105
  • 7
2
votes
1 answer

Abstract method implementation in component has undefined dependencies

Abstract class: export abstract class LanguageChangeAware { private sub: Subscription; protected language: string; protected constructor(protected eventService: EventService) { this.sub = eventService.getLang().subscribe(lang…
1
vote
0 answers

How to change provider useValue on runtime in Angular?

The Entry Point for my application is like www.mywebsite.com/context/token I have a Provider in AppModule which I use as a base path for all API calls : { provide: API_ENPOINT_BASE_PATH, useValue: 'rest/apis' } My Requirement is to read the token…
1
vote
2 answers

Why set up Dependency providers in angular?

I have created few services in angular with provideIn as root using the following code. @Injectable({ providedIn: 'root' } This makes the service available to the whole application and I can import it anywhere I like. This seems very easy to do…
1
vote
1 answer

Using service in app.routing.module Angular

Is there a way to inject and use a service in the route list in the Angular routing module in order to dynamically change route data? Like so: import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import {…
dzenesiz
  • 935
  • 3
  • 17
  • 40
1
vote
1 answer

How to make an HTTP call from useFactory function during provider configuration in Angular?

I have the next provider configuration { provide: APP_INITIALIZER, useFactory: initializeKeycloak, multi: true, deps: [KeycloakService], }, How to make an HTTP call from useFactory function…
1
vote
1 answer

Angular: dependency injection for different route

I have an Angular application with module-per-feature architecture. I have an OrdersModule with components like ListOfOrders, DetailOfOrder or EditOfOrder All of these components require OrdersApiService in constructor, which is basically a simple…
Luke1988
  • 1,355
  • 1
  • 10
  • 29
1
vote
1 answer

How to get elementRef without constructor injection? - Angular

I want to know if there is a way to get the ElementRef instance without the constructor injection in Angular? Why I need it: I have an abstract BaseComponent class that gets inherited in a lot of other Angular Component classes. I want to avoid…
Akash
  • 3,736
  • 3
  • 20
  • 38
1
vote
0 answers

'inject() must be called from an injection context' when importing services between Angular and Ionic project

I am trying to create a "simplest possible" mechanism for sharing code between an Angular web project & Ionic/Angular mobile app. I have also been exploring a monorepo approach, but thought I would see if it was possible to simply import modules…
1
vote
1 answer

Angular Unit Testing with Dynamic Component Failed with error TypeError: Cannot read property 'ɵcmp' of undefined

list of Angular dependency "@angular/common": "~9.1.1", "@angular/compiler": "~9.1.1", "@angular/core": "~9.1.1", "@angular/forms": "~9.1.1", "@angular/platform-browser": "~9.1.1", "@angular/platform-browser-dynamic":…
1
vote
1 answer

How to use provideIn: 'platform' in angular 9

I have been reading the angular 9 documentation, and it states you can use the provideIn: 'platform' to share a singleton service across two angular applications, but I was not able to find a good example and the documentation is not clear about how…
1
vote
1 answer

Angular2 Dependency Injection by Factory Method using Interface

I'm trying to use Angular2 dependency injection but get the following error message: error NG2003: No suitable injection token for parameter 'service' of class 'PaymentService' app.module.ts - provider with factory method @NgModule({ …
1
vote
1 answer

providedIn: LazyModule fails with no provider error

I would like to refer the providedIn option to an Angular module providing both declarables and dependencies. I created an additional module SharedModule to serve as a sub-module of the lazy module LazyModule so that it may be used as an “anchor”…
fekaloid
  • 173
  • 1
  • 1
  • 6
1
vote
2 answers

Default values config using forRoot works in Dev but not in Prod (Aot)

The following code works fine in Dev, but not in Prod mode: Module export class SpinnerModule { static forRoot(config?: SpinnerConfig): ModuleWithProviders { return { ngModule: SpinnerModule, providers: [SpinnerService, { provide:…