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

New instance of Angular singleton service being created in another singleton service

I am new to Angular and am trying to figure out how to inject my Angular service (game service) into another angular service which will be a Resolver (game resolver). Only one instance of my game service is typically created in my application and I…
1
vote
0 answers

Angular dependency injection of a component taking place without decorators

I came across some code in which some components have access to their parent component instance but I could not figure out how the "injection" takes place. I expected the code to fit in one of those scenarios, but there is no dedicated attribute…
Stphane
  • 3,368
  • 5
  • 32
  • 47
1
vote
2 answers

Provide Unique Service Instance in Every Component

I have a situation where I want to be able to have multiple widget components used on the page at the same time. I want to isolate the ContainerComponent dependencies so that each ContainerComponent instance references unique service instances. …
cobolstinks
  • 6,801
  • 16
  • 68
  • 97
1
vote
1 answer

Can't inject `Route` nor `ActivatedRouteSnapshot` into `HttpInterceptor`

Simplified version of the code: @Injectable() export class JwtInterceptor implements HttpInterceptor { constructor( private readonly router: Router, private readonly activatedRouteSnapshot: ActivatedRouteSnapshot, …
Zazaeil
  • 3,900
  • 2
  • 14
  • 31
0
votes
0 answers

polyfills.js:1 [webpack-dev-server] Server started: Hot Module Replacement disabled, Live Reloading enabled, Progress disabled, Overlay enabled

If i edit something in the webpage at that time angular application goes into infinite page load process for some mini, can any one help me with this issue. I was updating my angular project from 15 to 16 at that time i got this error while new…
0
votes
0 answers

No provider for w

How can I identify which service is not provided. I run ng serve to serve angular project. it is optimized and instead of service name in error message I have "w" or some other words I set providedIn property of @Injectable to…
0
votes
1 answer

How to inject the NGRX store in a functional angular resolver

I'm trying to convert my class-based resolver to a functional one. Injecting NGRX store is not working. export const loadCategoriesParentCategoryCodeResolver : ResolveFn = ( route, state, store: Store = Inject(Store)) =>…
Richard77
  • 20,343
  • 46
  • 150
  • 252
0
votes
0 answers

Use injectable dependencies in an angular module

I'm trying to generate generic reducers and actions of ngrx in Angular. I've already managed to do it but I have to apply the singelton pattern manually to generate the actions and reducers. class CertificateGroupReducer extends…
0
votes
1 answer

Inject components with a directive into a referenced element (Angular 12)

Now I inject my extra components to my main component's root level in a directive, with this approach: @Directive({ selector: '[my-controls]', }) export class MyControlsDirective implements OnInit, OnDestroy { constructor( …
0
votes
1 answer

Angular optional multi interceptors - why is it injected as null?

Given a multi injectable defined like below, without any providers, I would expect the injected list to be empty, but in reality it is null. Is there a way to inject the value as [] instead of null if there are no providers for this injectable? I…
Ákos Vandra-Meyer
  • 1,890
  • 1
  • 23
  • 40
0
votes
0 answers

Angular Same Service Instance in different Modules

so i have started to learn more about Dependency Injection. And as it was written in Docs, i expect that if i have 2 separate modules and both of them have their own provider in provider array. I'll get different singletone instances for both of the…
0
votes
2 answers

Angular - choose class to inject based on URL / logic available at bootstrap

(How) is it possible to configure injectors to use a class based on some data available at bootstrap? I have two implementations for interfacing with the backend, (for simplicity, let's say over http and over websockets). I created two service…
Ákos Vandra-Meyer
  • 1,890
  • 1
  • 23
  • 40
0
votes
1 answer

Errore while using APP_INITIALIZER function

I'm trying to load Firebase Remote Config using "APP_INITIALIZER" token. That's my code in app.module.ts: ... export function initSynchronousFactory(environmentService: EnvironmentService) { return () => { …
enfix
  • 6,680
  • 12
  • 55
  • 80
0
votes
1 answer

Can I re-provide a service for nested modal dialog component without overriding existing same token provided service?

If I inject a service 'Z' to component 'A' which spawns a dialog component 'B' which requires the service 'Z' to be a newly fresh copy of the service (provided in 'B' again). Will closing dialog 'B' "release" that nested service 'Z' so that…
0
votes
0 answers

Question about Angular Library dependencies

I have made an angular library (in Angular 12.2.12) which is importing CSS files from node_modules, but when I use it on my application, I got this error: GET http://localhost:60435/~@progress/kendo-theme-material/dist/all.css Here is the…