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

Angular8 - how to pass input to CDK Overlay by to Custom Injector with InjectionToken and ComponentPortal

I have 2 components: A home component and a confirmation component (overlay). When a button is clicked on the home component, it should trigger the confirmation component to open as a Angular CDK Overlay. The issue is I cannot pass anything from the…
2
votes
1 answer

How to inject parent component service into dynamic component in Angular?

I am using Angular 8 and I have dynamically created component. I have parent component with the service into providers array. How I can inject the same instance of parent MyService into a dynamic component? Without dynamic component I can achieve it…
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
  • 135
  • 1
  • 8
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…
2
votes
1 answer

How to inject a service into a class that must be instantiated in app module in angular 7?

I am using ngxs in a project and I want to use the ngxs logger plugin, but I want to override the logger itself so that I am able to log to the backend. For this I need to inject my RestService in my logger class but I am not being able to do so. I…
António Quadrado
  • 1,307
  • 2
  • 17
  • 34
2
votes
1 answer

Angular Universal, How to isolate module with declarations from server-side execution?

I have installed ng2-pdf-viewer to show documents within angular 6 app. To make it work I need to import PdfViewerModule in FeatureModule where I want to use component. The problem is in PdfViewerModule, it calls Element and Window…
2
votes
1 answer

How to override services injected to angular component

We're building a cms driven application framework with Angular. We deliver multiple packages that can be used by customers to further customise the ui or logic. Components to that reason will delegate their logic to a service, with the intend…
2
votes
2 answers

Invalid provider error when injecting service into service in Angular 2

I currently have a module setup like below (exerpt); AppModule RoutingModule AuthRouteGuard AuthModule LoginFormComponent AuthService I have defined my AuthService (responsible for handling user authentication and provides…
1
vote
0 answers

How does Angular distinguish/match injection tokens?

tl;dr: How does Angular know that an injection token passed to injector.get corresponds with an injection token of a particular provider? According to the Angular documentation, an InjectionToken can be used to inject interfaces. The example given…
1
vote
0 answers

How to share NgxPermissionsService instances across AppModule, Feature Module and a Library Module

I'm trying to use ngx-permissions to manage my app permissions. My app is depending on a library to share some features to different apps and the permissions will be loaded by this library. the problem here is, the library has a different instance…
1
vote
0 answers

Angular how to provide and overwrite a service with "useClass"?

I have problems to "overwrite" a service with provide: ... useClass: .... There is a component EditComponent that uses a service SettingsService. See below: @Injectable( { providedIn: 'root', }) export class SettingsService { public test:…
Lars
  • 920
  • 1
  • 14
  • 34
1
vote
4 answers

Inject parent FormArray into child component in Angular 14

In my parent component, I have a FormGroup with a FormArray, and I want to handle that array in a child component. The parent's HTML does this: I assumed in the child…
1
vote
2 answers

How does Angular components re render when data is updated?

I would like to understand how an Angular component re-render to display the new information whenever there is a change in the underlying data. (eg. push/delete) I have the following component that uses a data service to get the data. When the…
1
vote
1 answer

Lazy Singleton-Service in multiple lazy modules

I have a Service which I would like to provide to 2 Lazy Modules - but not for the whole application. I'm Providing the Service through a Module with the forRoot(): ModuleWithProviders convention into those 2 Lazy Modules - which leads to 2…
xanx2323
  • 11
  • 1
1
vote
0 answers

Angular guard instantiated before injected service

I have an Angular guard that uses data from injected service to determine if a route can be accessed, like so: @Injectable({ providedIn: 'root' }) export class MyGuard implements CanActivate { constructor(private someService: SomeService,…
dzenesiz
  • 1,388
  • 4
  • 27
  • 58