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.

NgComponentOutlet should allow module injectors in addition to component injectors

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
angular-robot[bot]commented, Jun 4, 2021

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.

0reactions
angular-automatic-lock-bot[bot]commented, Apr 14, 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

NgComponentOutlet - Angular
ngComponentOutletInjector : Optional custom Injector that will be used as parent for the Component. Defaults to the injector of the current view container....
Read more >
Choose injector when creating components #37857 - GitHub
Specify the injector when creating the component. ... child component, and it requires explicitly adding components to module entry points.
Read more >
angular - *ngComponentOutlet - use the injector of the ...
I search through all the Angular documentation but I am very new to Angular, so I was not able not figure out how...
Read more >
Pass an Injector to Embedded Views | by Netanel Basal
We're creating a new provider in the component injector and passing the injector to the new ngTemplateOutletInjector directive. Now BarComponent will use ...
Read more >
Inject dynamic content in Angular components with portals
Learn how to dynamically render content in your Angular components using Angular CDK portals in this in-depth tutorial.
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