Skip to content

Cannot inject Renderer2 when the application uses Renderer3! #44897

Closed

Description

@tk2rush90

Which @angular/* package(s) are the source of the bug?

core, forms

Is this a regression?

No

Description

image

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

Activity

  1. added
    area: core
    on Jan 30, 2022
  2. added this to the needsTriage milestone on Jan 30, 2022
  3. added
    needs reproduction
    on Jan 30, 2022
  4. modified the milestones: needsTriage, Backlog on Jan 30, 2022
  5. AndrewKushnir commented on Jan 30, 2022

    @AndrewKushnir
    Contributor

    @tk2rush90 could you please create a repro using Stackblitz, so that we can investigate this issue? Thank you.

  6. tk2rush90 commented on Jan 31, 2022

    @tk2rush90
    Author

    @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 the SearchComponent, then the error doesn't appear.

    image

  7. tk2rush90 commented on Jan 31, 2022

    @tk2rush90
    Author

    image

    When calling the createComponent() method with ComponentFactory, the error doesn't appear.
    I think the injected Renderer2 in IconComponent is the cause of this error.

    image

  8. atscott commented on Jan 31, 2022

    @atscott
    Contributor

    @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?

  9. added
    P3
    and removed
    needs reproduction
    on Feb 1, 2022
  10. alxhub commented on Feb 1, 2022

    @alxhub
    Member

    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 creates Renderer3 (that is, document) is used. Exactly this happens for the dynamically instantiated component in the example. That component then attempts to inject Renderer2, which causes the crash.

    So why is RendererFactory2 missing? Firstly, the example app creates an injector via Injector.create([...]). This injector doesn't inherit from the app injector, so it doesn't have a binding for RendererFactory2. This didn't used to be a problem, however, since ViewContainerRef.createComponent() tries to create a chained injector using the given injector and the component's NgModuleRef. Even if the given injector was missing RendererFactory2, the NgModuleRef (usually the app injector or derived from it) would have it!

    Where did the NgModuleRef come from? It's not common to explicitly pass that argument to ViewContainerRef.createComponent(). Previously that API required a ComponentFactory, though, which has an internal NgModuleRef obtained from its ComponentFactoryResolver.

    In a recent Angular though, we introduced a factory-less API for createComponent(), which made it easy to call the API with no NgModuleRef. So the only tokens available for injection when creating this component are those in the user-provided injector, which does not include RendererFactory2. Therefore, Renderer3 gets selected, and the component crashes!

  11. tk2rush90 commented on Feb 1, 2022

    @tk2rush90
    Author

    @alxhub Thank you for helping! You're my savior!

  12. alxhub commented on Feb 1, 2022

    @alxhub
    Member

    @tk2rush90 a workaround would be to inject Injector inside your modal service, and use that as the base of the Injector.create call when you create the injector for whatever modal component is being displayed.

  13. self-assigned this
    on Feb 3, 2022
  14. added a commit that references this issue on Feb 8, 2022
    92cf9fb
  15. angular-automatic-lock-bot commented on Mar 11, 2022

    @angular-automatic-lock-bot

    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.

  16. locked and limited conversation to collaborators on Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area: corecore: dicore: dynamic view creationstate: has PRtype: bug/fix

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions