Repository navigation
Closed
Labels
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: coreIssues related to the framework runtimecore: dicore: dynamic view creationstate: has PRtype: bug/fix
Milestone
Description
Which @angular/* package(s) are the source of the bug?
core, forms
Is this a regression?
No
Description
In my code, I was injecting some providers when calling createComponent() method of ViewContainerRef. It worked well.
But after upgrading Angaulr version from 13.1 to 13.2, I got 'Cannot inject Renderer2 when the application uses Renderer3!' error .
The code works well if I remove the options for createComponent() method, however, I don't want to remove that.
Please help with this issue 😭
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
core.mjs:6468 ERROR Error: Cannot inject Renderer2 when the application uses Renderer3!
at getOrCreateRenderer2 (core.mjs:21014:1)
at injectRenderer2 (core.mjs:21025:1)
at Renderer2.__NG_ELEMENT_ID__ (core.mjs:21009:1)
at getOrCreateInjectable (core.mjs:3383:1)
at Module.ɵɵdirectiveInject (core.mjs:14389:1)
at NodeInjectorFactory.DefaultValueAccessor_Factory [as factory] (forms.mjs:254:1)
at getNodeInjectable (core.mjs:3548:1)
at searchTokensOnInjector (core.mjs:3485:1)
at getOrCreateInjectable (core.mjs:3429:1)
at ɵɵdirectiveInject (core.mjs:14389:1)
defaultErrorLogger @ core.mjs:6468
handleError @ core.mjs:6515
handleError @ core.mjs:10940
executeListenerWithErrorHandling @ core.mjs:14963
wrapListenerIn_markDirtyAndPreventDefault @ core.mjs:14998
(익명) @ platform-browser.mjs:466
invokeTask @ zone.js:406
onInvokeTask @ core.mjs:25459
invokeTask @ zone.js:405
runTask @ zone.js:178
invokeTask @ zone.js:487
invokeTask @ zone.js:1600
globalZoneAwareCallback @ zone.js:1626
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 13.2.0
Node: 14.17.0
Package Manager: npm 6.14.13
OS: win32 x64
Angular: 13.2.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1302.0
@angular-devkit/build-angular 13.2.0
@angular-devkit/core 13.2.0
@angular-devkit/schematics 13.2.0
@schematics/angular 13.2.0
rxjs 7.4.0
typescript 4.5.5
Anything else?
No response
Metadata
Metadata
Assignees
Labels
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: coreIssues related to the framework runtimecore: dicore: dynamic view creationstate: has PRtype: bug/fix

Activity
AndrewKushnir commentedon Jan 30, 2022
@tk2rush90 could you please create a repro using Stackblitz, so that we can investigate this issue? Thank you.
tk2rush90 commentedon Jan 31, 2022
@AndrewKushnir
Here is the link:
https://stackblitz.com/edit/github-jc32pa?file=src%2Fapp%2Fsacheon-bus%2Fcomponents%2Fbus-search%2Fbus-search.component.ts
Please check the comment. Thanks.
More Info
I found that if I remove the
[ngModel],(ngModelChange), and<app-icon></app-icon>from theSearchComponent, then the error doesn't appear.tk2rush90 commentedon Jan 31, 2022
When calling the
createComponent()method withComponentFactory, the error doesn't appear.I think the injected
Renderer2inIconComponentis the cause of this error.atscott commentedon Jan 31, 2022
@tk2rush90 The demo link does not run on stackblitz. I also tried downloading, installing dependencies, and running with the CLI and that also didn't work. Can you please provide a working reproduction?
alxhub commentedon Feb 1, 2022
I was able to get the example working and reproduce the problem as described. Here's a trivial reproduction: https://stackblitz.com/edit/ng13-broken-renderer3?file=src/app.ts
It's actually a fascinating bug, and is a combination of several factors:
At the root of the issue is an assumption Angular makes: if the injector being used to create a component doesn't have a binding for
RendererFactory2, then a factory that createsRenderer3(that is,document) is used. Exactly this happens for the dynamically instantiated component in the example. That component then attempts to injectRenderer2, which causes the crash.So why is
RendererFactory2missing? Firstly, the example app creates an injector viaInjector.create([...]). This injector doesn't inherit from the app injector, so it doesn't have a binding forRendererFactory2. This didn't used to be a problem, however, sinceViewContainerRef.createComponent()tries to create a chained injector using the given injector and the component'sNgModuleRef. Even if the given injector was missingRendererFactory2, theNgModuleRef(usually the app injector or derived from it) would have it!Where did the
NgModuleRefcome from? It's not common to explicitly pass that argument toViewContainerRef.createComponent(). Previously that API required aComponentFactory, though, which has an internalNgModuleRefobtained from itsComponentFactoryResolver.In a recent Angular though, we introduced a factory-less API for
createComponent(), which made it easy to call the API with noNgModuleRef. So the only tokens available for injection when creating this component are those in the user-provided injector, which does not includeRendererFactory2. Therefore,Renderer3gets selected, and the component crashes!tk2rush90 commentedon Feb 1, 2022
@alxhub Thank you for helping! You're my savior!
alxhub commentedon Feb 1, 2022
@tk2rush90 a workaround would be to inject
Injectorinside your modal service, and use that as the base of theInjector.createcall when you create the injector for whatever modal component is being displayed.fix(core): `ViewContainerRef.createComponent` should consult module i…
angular-automatic-lock-bot commentedon Mar 11, 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.