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

107 questions
1
vote
1 answer

Angular TestBed.inject

I am doing angular unit testing. Does TestBed.inject(service) create a new instance of the service? I was always under the assumption that TestBed.configureTestingModule() created the service and by inject we just accessed a reference of the created…
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

How to inject directive of subclass into component

I have a component that should change its behavior depending on what directive is added to the element. This custom logic are defined in the directives so the component needs not be modified when a new scope/feature is…
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,850
  • 2
  • 24
  • 42
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
3 answers

Angular dependency with "providedIn" in lazy loading modules

I am using Angular "Lazy-loading feature modules" as an example: live demo CustomersModule is a lazy loading module i create a test service in customer module. const routes: Routes = [ { path: 'customers', loadChildren: () =>…
Eddy Shan
  • 21
  • 1
  • 3
1
vote
2 answers

Dependency inject components in Angular

Is it possible to somehow dependency inject components in Angular? I would like to be able to do something similar to what you can do with services e.g.: my.module.ts: providers: [ { provide: MyService, useClass: CustomService …
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
  • 195
  • 1
  • 2
  • 9
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:…
1
vote
0 answers

Decide at startup time which implementation to use for a service provided via Dependency Injection

Let's assume I have a Front End App which needs to fetch data from some backend. And let's assume that there are 2 different backend REST APIs implementations available, BackEnd1 and BackEnd2. They both offer the same semantics, so the client can…
Picci
  • 16,775
  • 13
  • 70
  • 113
1
vote
1 answer

Angular 8 inject pipe into another pipe does not work

After upgrading from Angular 7 to Angular 8 my pipe stopped to work with error: TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) 10 | export class APipe implements PipeTransform { 11 | > 12 | …
Felix
  • 3,999
  • 3
  • 42
  • 66
1
vote
2 answers

Angular inject a component into an attribute directive

Tl;dr: How do I provide a visible component as a dependency for a directive? Naturally the component has to get initialized before the directive, but it has to be the same instance that gets displayed when the app later runs across the selector of…
1
vote
1 answer

Angular update 8.1 - circular dependency warning

Since updating to Angular 8.1 (8.0 worked), I'm getting a circular dependency warning that doesn't really make sense to me. I have an Injectable Service SelectService that is provided in some components. The service is using a class SelectableItem.…
Thomas Schneiter
  • 1,103
  • 2
  • 19
  • 35
1
vote
1 answer

Use an angular service which is not injected anywhere

Suppose you have an Angular service (ZombieService) which, for example, monitors an other service. And, ZombieService isn't injected anywhere DEMO The problem is that, when you do not inject a service anywhere, that service is completely ignored…
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
1
vote
0 answers

How to proxy a component that uses DI?

I'm trying to create a proxy for Angular components. Starting from this solution: https://github.com/Microsoft/TypeScript/issues/4890#issuecomment-141879451 I ended up with this: interface Type { new (...args): T; } interface Base…
Manuel
  • 10,869
  • 14
  • 55
  • 86