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
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 { …
Andrew
  • 1,507
  • 1
  • 22
  • 42
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: pass service from custom element to child custom element

I have build a demo which creates 2 custom angular elements (checkout app.module). Works like a charm, but there is one problem, if I provide a service in the parent element (called BarComponent), it is not received by the its child CE (called…
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
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
  • 6,403
  • 14
  • 79
  • 173
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,473
  • 1
  • 16
  • 32
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
  • 5,315
  • 11
  • 46
  • 79
0
votes
1 answer

Injecting different parent components into child component

Im trying to solve the following situation: I have a child component that will be used in multiple parent components but this child component needs to have access to the parent component (namely its parenComponent.constructor information at…
0
votes
1 answer

How do I refer to my config in my imports?

I have added AngularFire to my project. In its readme I was instructed to add the AngularFireModule.initializeApp(...) to the imports of my AppModule In examples people use the environment constant, however, that is not an option because Deployment…
0
votes
1 answer

Angular - Providing asynchronous to root providers

My goal is to load a configuration file (from assets) which includes settings I need for my http service. Currently I have an APP_INITIALIZER service which loads the file and sets it in the service properties. Thing is, the http service is…
0
votes
0 answers

Pass string value in providers and pass value to super of Base Service

I am creating a generic crud service that i want to use throughout my angular application in various feature modules. To achieve this i need to pass a string value to a service in providers: of an angular module, so i can then set a value to the…
ccocker
  • 1,146
  • 5
  • 15
  • 39
0
votes
1 answer

Transitively inject injection token into a service defined in library, used within an angular application

I've created an angular library, say library1, which defines and exposes a service APIService. The APIService has a constructor argument (URL: string), which is injected with an Injection token defined as, const URL_TOKEN = new…
Sanjay Verma
  • 1,390
  • 22
  • 40
0
votes
1 answer

Get value for provider from service

I am trying to assign some value (which I got from http request) from another service to provider token in app.module.ts. My service looks like this: @Injectable() export class AppConfigService { private appConfig: AppConfig; …
0
votes
1 answer

Angular service global injection

Is there a way to access a field of a service without injecting the service into every component? or equivalently: does some concept of 'global' service exist in Angular 6? My use case is as follows: Text labels for fields are retrieved at…
t_T_t
  • 23
  • 3