Angular Dependency Injection: Individual instances of each service are provided for every module usage

Within my application, there is a module called "FileUpload". The upload service included is "UploadService", which receives a service injection with the interface "RequestService."

I also have two other modules: FileManager1 and FileManager2. Each file manager offers its own implementation of a service with the interface "RequestService" and provides it under the token "REQUEST_SERVICE."

To demonstrate this scenario in a simplified manner, I have created a StackBlitz example:

In order to create a separate instance of the file-upload service for each module usage, I made the decision to move the service provider from the file-upload.module.ts to the component drop-zone.component.ts. This adjustment has proven successful as evidenced by the console.log.

Clicking on "Click me" currently yields the same output for FileManager1 and FileManager2 ("Url2"). However, the desired outcome is "Url1" for FileManager1 and "Url2" for FileManager2.

I am seeking a clean solution to meet this requirement without resorting to multiple tokens apart from "REQUEST_SERVICE."

Thank you for your assistance!

Output displayed after click events:

A new service has been created....
A new service has been created....
Angular is running in development mode. Call enableProdMode() to enable production mode.
Upload file to:
Url2
Upload file to:
Url2

Desired output should be:

A new service has been created....
A new service has been created....
Angular is running in development mode. Call enableProdMode() to enable production mode.
Upload file to:
Url1
Upload file to:
Url2

Answer №1

In Angular, ModuleInjectors are only created for the RootModule(AppModule) and lazy-loaded modules. When directly importing modules, the providers are elevated to the first lazy-loaded module in the hierarchy or to the RootModule if there isn't one.

If you import both FileManager1Module and FileManager2Module directly into AppModule in that order, Angular will first elevate the provider for REQUEST_SERVICE from FileManager1Module to AppModule, and then overwrite it with the one from FileManager2Module. This explains why "Url2" is printed to the console.

To resolve this issue, you have two options:

  • Declare REQUEST_SERVICE as a component level provider (within FileManager1Component and FileManager2Component)
  • Or if you intend to use the Router, consider lazy-loading the modules.

Cheers!

Answer №2

One potential solution is to define a distinct RequestService for each module provider in the following manner:

@Injectable({
  providedIn: CustomModule1 ,
})
export class CustomRequestService1 implements RequestService{
}

@Injectable({
  providedIn: CustomModule2 ,
})
export class CustomRequestService2 implements RequestService{
}

Then, you can easily inject these services by using the RequestService interface within any component or service of those respective modules.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering npm3 installation errors with typyings in Angular 2?

Each time I try to sudo npm install Angular 2 modules, everything updates and installs correctly. However, I encounter the following error when the typings install is attempted: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0 ...

End-to-End Testing in Angular 2

Recently, I embarked on my Angular 2 journey and started exploring e2e testing. One challenge I encountered was trying to close a Bootstrap 4 modal by clicking in a blank area using Protractor. I attempted the following methods: it('should close the ...

Exploring Angular's ability to utilize the Map and Subscribe functions within an

I could use some assistance with RxJS. I have a piece of code that is supposed to fetch an API and then, for each returned element, retrieve the type of that element from another API. The code is functioning properly, but it returns an Observable for the ...

Using Angular to call a separate function from within a click event handler within the same function itself

It seems like the issue at hand is related to the keyword "this" not being assigned as expected. I am currently using Angular 5 along with a WordCloud package. You can find more information about it here. There's a click callback function that retur ...

Is there a method to enable the acceptance of both dots and commas as decimal markers in input fields?

Hey there! I have a question for you. Is there a way to allow users to input both commas and dots as decimal markers, but still display the value with dots and remove thousand separators? For example, when a user inputs "123456,78", they should see "123 ...

Guide on displaying tooltip messages for a custom directive in Visual Studio Code

I have developed a custom directive called app-subscriber. When I hover the mouse over it, I want to display a tooltip message saying "This is for subscribers and requires an email address". Is this achievable? Do I need to create a new VS Code extension f ...

Steps for Signing Up for a Collaboration Service

I'm struggling to understand how to monitor for new values in an observable from a service. My ultimate objective is to integrate an interceptor, a service, and a directive to show loading information to the user. I have set up an interceptor to liste ...

Component presenting surprising results

Struggling to display data in an HTML component, I encountered a peculiar issue. Upon entering values for the first time, everything appears correctly. However, upon subsequent entries and retrievals, the second value is displayed twice, the third value th ...

"Exploring the world of 3rd party libraries in Angular2 with Typescript and Webpack

I've begun working on a fantastic seed project that can be found at: https://github.com/AngularClass/angular2-webpack-starter However, I've encountered an issue with integrating third-party modules. Can anyone offer guidance on how to properly a ...

generate a fresh array with matching keys

Here is an example array: subjectWithTopics = [ {subjectName:"maths", topicName : "topic1 of maths " }, {subjectName:"maths", topicName : "topic2 of maths " }, {subjectName:"English", topicName : &quo ...

Exploring the dynamics of parent and child components in Angular

I'm currently working on an Angular2 project and I've hit a roadblock with the parent/child component interaction. My goal is to have a "Producer" entity that can have multiple "Products". Ultimately, I aim to retrieve lists of products associat ...

What is the reason behind the NgForOf directive in Angular not supporting union types?

Within my component, I have defined a property array as follows: array: number[] | string[] = ['1', '2']; In the template, I am using ngFor to iterate over the elements of this array: <div *ngFor="let element of array"> ...

Retrieving array-like form data in a TypeScript file

I need assistance with passing form inputs into a typescript file as an array in an ionic application. The form is located in question.page.html <details *ngFor="let product of products;"> <ion-input type="text" [(ngModel ...

Setting up Firestore with @angular/fire 17 and Ionic @ionic/angular 7.6.2 for full offline functionality: a step-by-step guide

Recently, I've delved into app development using Angular, Ionic, and Firebase. Currently, I'm grappling with the challenge of setting up offline capabilities/unlimited cache size with AngularFire, and it's been days of confusion and dead-en ...

Effective strategies for extracting value from asynchronous Angular events that return Promises

When it comes to subscription blocks, extracting the value from my API is possible. var data = null; this._timinServiceProxy.getDateFromNTP().subscribe(result => { data = result; console.log(data); // The expected result }); console.log(data); ...

Using Arrow Functions in Angular 2 Typescript with Support for IE11

Are arrow functions in Typescript for Angular2 compatible with IE 11? I have come across information stating that arrow functions in javascript may not be supported in IE 11, but I am uncertain if the same applies to Typescript. ...

Incorporating an external SVG file into an Angular project and enhancing a particular SVG element within the SVG with an Angular Material Tooltip, all from a TypeScript file

Parts of the angular code that are specific |SVG File| <svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="950" height="450" viewBox="0 0 1280.000000 1119.000000" preserveAspectRatio= ...

What materials are required in order to receive messages and information through my Contact page?

Currently, I am pondering the optimal method for gathering information from my Contact page. I have already created a form; however, I'm unsure how to send the gathered data to myself since I am relatively new to Web development. Angular is the framew ...

A method for comparing two arrays containing identical objects and then storing the results in a variable

I have an item stored within two other items called formKeyValues and form formKeyValues https://i.stack.imgur.com/nRfiu.png form https://i.stack.imgur.com/eDpid.png I am looking to extract only the keys and values from formKeyValues and place them in ...

What could be the reason for the absence of the Froala editor display

I integrated the Froala editor into my Angular2 project but it doesn't seem to be visible on the screen, and there are no errors present. I made sure to import the necessary modules into app.module.ts, included jQuery in main.ts, and added the scripts ...