question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Confusion on 'providedIn' for a specific NgModule

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ X  ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

This is from the doc

The @Injectable() decorator has the providedIn metadata option, where you can specify the provider of the decorated service class with the root injector, or with the injector for a specific NgModule.

Providing a service with the app's root injector is a typical case, and the CLI sets up this kind of a provider automatically for you when generating a new service. However, you might not always want to provide your service at the root level. You might, for instance, want users to explicitly opt-in to using the service.

Instead of specifying the root injector, you can set providedIn to a specific NgModule.

For example, in the following excerpt, the @Injectable() decorator configures a provider that is available in any injector that includes the HeroModule.

What I understand from the doc, when you use providedIn: 'root' in your service, you don’t have to configure this service at the app module level. And the service is available anywhere the class is imported.

But if I use providedIn: HeroModule, I thought I didn’t have to configure this service at the HeroModule level and I could use this service in HeroComponent (HeroComponent declared in HeroModule). This is not the case. And I still need to configure that service in HeroModule.

So What is this providedIn: SpecificModule for?

here is the demo

Expected behavior

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Angular version: 6.*.*


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:5
  • Comments:41 (21 by maintainers)

github_iconTop GitHub Comments

24reactions
trotylcommented, Sep 2, 2018

Just don’t do that.

16reactions
manklucommented, Sep 4, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 6+ :ProvidedIn a non root module is causing a circular ...
I can fix this by just providing the translationsResolver as a NgModule provider (in the providers array) but I prefer it to be...
Read more >
ProvidedIn FeatureModule Is A Confusing Concept For Me In ...
Ben Nadel gets confused by the "providedIn: FeatureModule" semantics in the @Injectable() meta-data in Angular 6.1.9; and, discovers that ...
Read more >
Angular Services, providedIn and Lazy Modules - | juri.dev
Let's explore some of the particularities of the Angular dependency injection mechanism.
Read more >
Angular Services, providedIn and Lazy Modules
Remember, if you some things are confusing - you can always hire an Angular ... that a service should be provided in a...
Read more >
Total Guide To Angular 6+ Dependency Injection - Medium
Lazy @NgModule. Instance of the service provided in the lazy module will be created on the child injector (of the lazy module) when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found