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
0
votes
1 answer

Data from the service not available when accessed from the same level to app in Angular

I have a problem related to dependency injection. I am new to Angular, the problem is that, I tried to store the logged-in user data in service and call this service in the app component that gets logged in user information from API. The data from…
0
votes
1 answer

How to provide an implementation for a given class using DI in Angular?

Consider that I have 2 modules called A and B. Let's say I have this class. It doesn't really "belong" to any module since it's a plain old Typescript class rather than an Injectable. class Greeter { constructor(private greeting: string) {} …
Allan Juan
  • 561
  • 4
  • 16
0
votes
1 answer

How to inject object that has constructor parameters in Angular?

I'm very to Angular and I want to figure out how can I pass parameters to an object constructor that is being injected into a component. Here is an example: image-capture.service.ts @Injectable({ providedIn: 'root'}) export class ImageCapture { …
0
votes
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…
0
votes
1 answer

How to provide and inject functions in angular?

I have the function that I need to inject in any component: export const matDialogCallbacks = (dialog: MatDialog, document: Document, renderer: Renderer2) => { dialog.afterOpened.subscribe(() => renderer.addClass(document.body,…
user14832549
0
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…
0
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"); …
0
votes
0 answers

Angular: Read json file and inject content to module as dependency

I have an angular project with lazy loaded modules with Core and shared modules. Each module has some json configuration files that i need to use their contents in the components with dependency injection. Which walkthrough is better for this: 1-…
0
votes
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
  • 1
  • 1
0
votes
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 …
0
votes
2 answers

How to specify angular service injection strategy?

I have service like folowing: @Injectable({ providedIn: 'root' }) export class BoxContainerService { private boxBehaviour: BehaviorSubject = new BehaviorSubject([]); boxes$ = this.boxBehaviour.asObservable(); addBox(b:…
barteloma
  • 5,342
  • 7
  • 55
  • 126
0
votes
0 answers

Angular 9 Service as Obesrvable in library

I am working on multiapplication setup, where inside workspace 2 or 3 applications will share library with common and shared components. Currently, I am adding notifications service. Added (at least for now) as part of library main module. As base I…
0
votes
1 answer

Services reset on logout in Angular

I have some services in my app which have data that I'd like to reset when the user logs out. I have a parent component that is only shown when the user is logged in so if I include those services in the providers array of that component decorator,…
Pizzicato
  • 1,174
  • 1
  • 10
  • 26
0
votes
1 answer

Angular 8 Import Service into Interface/Module

I have a service, called ConfigService in config.serivce.ts, i need to get a function (getClientID) that returns a value and import this value into a module file: appAuth.module.ts I need to know how to inject a value from a service function in…
0
votes
0 answers

How to access DI from model in a generic way?

I have the following class (only the relevant part): import { AppInjector } from '@app/shared/utility/app-injector'; import { HttpService } from '@app/shared/services/http.service'; export class Lazy { private readonly _http: HttpService =…
ssougnez
  • 4,202
  • 9
  • 36
  • 65