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.

NgModule should be able to declare providers/service they need.

See original GitHub issue

I’m submitting a …

[ ] bug report 
[x] feature request
[ ] support request

Current behavior NgModule cannot declare providers/service they need. It can only provide a service for global instanciation. I see only two options :

  1. The provider/service is defined above the NgModule, and it can be used in the NgModule. The service is shared according to scope, but this requiers the service to be instanciated even though it is never going to be used. I find this in contradiction with the goal of dynamic/lazy loading. Also An application must be aware of what service are needed by its underneath NgModules.
  2. The provider/service is provided to the application with NgModule.providers and it is instanciated globally… If another NgModule provides the same Service, the service will be reinstanciated caussing possibly two instances. Also if the NgModule is loaded dynamically by route, the service is reinstanciated each time the route is activated. I find this option in contradiction with the idea of sharing a Service for multiple usage.

As a result, NgModule.providers is not really usable except to instanciate global service the NgModule defines or to instanciate local service when loaded dynamically.

Expected/desired behavior Wouldn’t it be great if an NgModule could just use existing service or create a new global instance if not yet created ? what do you think ? I sugest :

  1. The addition of a new descriptor (NgModule.uses or NgModules.services or NgModule.injects) to list all Services that are requiered by the NgModule to work. Implementation would inject a new Service of that class or inject yet created instance. This would allow for late loading/instantiation of Services and save boot time.
  2. The renaming of providers into provides to make it clearer what this is.

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

  1. No need to declare service before they are actually needed.
    • main.module.ts is easier to read because there are only the providers/services necessary at bootime.
    • developpers don’t need to check services needed by NgModules they don’t know.
  2. NgModules can declare providers they need without conflicts.
    • module make it cleat what providers they need.
    • module make it clear what service they provide.
  3. Service can be loaded and reused dynamically.
    • Modules loaded dynamically can declare a local service they can latter reuse.
    • Service is instanciated only when it is actually needed (by the first dynamically loaded NgModule)
  4. Better distribution of code.
    • say Two NgModule XModule1 and XModule2 share data through a service XService that is of no use to the rest of the application. XModule1, XModule2 and Xservice can be better distributed in one bundle and XService don’t need to be statically referenced in application code.

Please tell us about your environment:

  • Angular version: 2.0.0-rc.5

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
DaSchTourcommented, Sep 6, 2016

Well, I thought that the current implementation is exactly as this request suggests. I hope that this can be changed very soon. At the moment RC5 and later feel a lot like a alpha version. The breaking changes are gigantic and there is so much to improve. I’m currently putting all Angular 2 development on hold. The direction Angular is heading to is hard to determine.

I’d really love to see this feature in RC7 together with a stable and easy usable module/component system so I can go on with my Angular 2 projects.

0reactions
angular-automatic-lock-bot[bot]commented, Apr 4, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Providing dependencies in modules - Angular
A provider is an instruction to the Dependency Injection system on how to obtain a value for a dependency. Most of the time,...
Read more >
Angular 2 - Service as NgModule to not have to declare all ...
I don't need to access all the providers declared in the app module directly. The only class in need is the CalculationService but...
Read more >
NgModule.providers vs Component.providers vs ... - codecraft.tv
We can configure a provider on the NgModule, on a component's or directive's providers property, and on a component's viewProviders property. Deciding where...
Read more >
Dependency Providers - Angular
The injector relies on the provider configuration to create instances of the ... You must configure an injector with a provider, or it...
Read more >
Angular Modules and NgModule - Complete Guide
Introduction to Angular NgModule, modularity, and lazy-loading in Angular. ... To make it publicly available, we need to export it: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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