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
4
votes
2 answers

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
  • 8,245
  • 4
  • 49
  • 65
4
votes
0 answers

Is there a way to enforce typesafety for Angular providers with dependency injection?

I would like to use dependency injection to swap the implementation of the provider but can't figure out how to make that type safe. @Injectable() class ExampleService { exampleMethod(): string { return 'test'; } } // Note that…
bogeylicious
  • 5,071
  • 3
  • 25
  • 17
3
votes
2 answers

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
  • 1,388
  • 4
  • 27
  • 58
3
votes
2 answers

Multi implementation of an abstract class in typescript for Angular dependency injection

I have an abstract class and an implementation of it export abstract class IPrint { abstract Print(textInput: string): void; } export class FilePrint implements IPrint { Print(textInput: string): void { console.log("File Print"); …
Mina Mohammadi
  • 285
  • 1
  • 4
  • 14
3
votes
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…
2
votes
0 answers

How do I transfer generically typed data using dependency injection?

My application displays data about different kinds of business objects in the same standard component. That is, let's assume there is an Angular component type EditorComponent. This component is instantiated using a component…
2
votes
1 answer

What is the injection context in Angular 16+

Im faced with a term called "Injection context", and trying to figuring out what it actually is. Coz in angular we have following things that somehow related with injection context (listed below): EnvironmentInjector#runInContext injectionContext…
2
votes
2 answers

Angular - useExisting ngForm optional

I need to inject the form from a parent component into a child component to be able to validate the child component input like so:
2
votes
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…
2
votes
2 answers

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
  • 4,412
  • 4
  • 30
  • 48
2
votes
2 answers

Angular 9 - how to inject dynamic parameters into a service constructor

I need to make requests to a backend url which comes in this form: localhost:8000/myapp/item1/:id1/item2/:id2/item3 where id1 and id2 are dynamic numbers. I've thought using a service that takes 2 arguments in the constructor, something like…
2
votes
1 answer

Inject query parameter into dependency provider factory in Angular

I want a value from the window.location search parameters passed to the body of a dependency provider factory, ideally in an idiomatic Angular way. Use case: writing my first Angular app I have the app running on one port and the backend server on…
2
votes
1 answer

How to pass attribute Directive instance to nested component using Dependency Injection in Angular

I'm trying to use an attribute Directive to pass an element's template reference from a parent component down to a nested component using dependency injection, but the Directive instance in the parent component that gets a hold of the target element…
2
votes
0 answers

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":…
2
votes
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({ …