NgComponentOutlet should allow module injectors in addition to component injectors
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[x] Feature request
[ ] 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
Current behavior
NgComponentOutlet allows the user to specify the injector to be used when instantiating and inserting a component using an NgModuleFactory, but it currently assumes that the injector being passed is a component injector, not a module injector. This leads to restrictions when the injector passed to NgComponentOutlet has additional services added via ReflectiveInjector.resolveAndCreate
, as the module containing the component cannot access these providers. This limitation appears to have been introduced with pull request #15044 and occurs because of the assumption at https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_component_outlet.ts#L89.
Expected behavior
NgComponentOutlet should either detect or allow the specification of whether the injector provided is a component or module injector. If it is a component injector, the behavior should remain unchanged. If it is a module injector, then NgComponentOutlet should not attempt to retrieve the NgModuleRef’s injector using the custom injector, as this retrieves the new module injector’s parent, discarding any newly added providers.
What is the motivation / use case for changing the behavior?
This limitation prevents a parent component from dynamically loading several components with different custom providers accessible at the module level. This introduces unnecessary complexity for users writing dynamically loaded modules, as providers created via ReflectiveInjector.resolveAndCreate
within the parent component are only accessible to providers in the dynamic component’s component injector tree (not the module injector tree).
For example, if I declare a service as a provider in my custom module, it will not be able to access any additional providers I create for the NgComponentOutlet’s injector via ReflectiveInjector.resolveAndCreate
. Only providers within the component tree can access them.
Environment
Angular version: 4.3.1
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.
Find more details about Angular’s feature request process in our documentation.
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.