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.

ComponentRef Change detector doesn't trigger detecting for view

See original GitHub issue

šŸž bug report

Affected Package

The issue is caused by package @angular/core

Is this a regression?

Cannot say

Description

Component created by #ComponentFactoryResolver. ComponentFactoryResolver#createComponent returns ComponentRef that does have changeDetectorRef.
After any changes to component’s input by code (not by template) and invocation of such ComponentRef#changeDetectorRef.detectChanges component’s view won’t be updated.

example from repro: dynamic-wrapper.component.ts#createComponent.

componentRef.changeDetectorRef.detectChanges()

This is root issue.

To testing purposes ChangeDetectorRef was be added to component’s constructor and invoked by consumer of this ComponentFactoryResolver. CDR from component’s injector works as expected

example from repro: dynamic-wrapper.component.ts#createComponent

componentRef.instance.cdr.detectChanges()

šŸ”¬ Minimal Reproduction

StackBlitz

GitHub repro repository: https://github.com/RolginRoman/ng-dynamic-components-cdr-repro

šŸ”„ Exception or Error


No exception

šŸŒ Your Environment

Angular Version:



 Angular version 9.1.0

Anything else relevant?

Seems like browser, OS version or anything else doesn’t matter

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
montella1507commented, Jul 29, 2020

Workarroud Is to use component’s injector to get Real ChangeDetectorRef.

ComponentRef.injector.get(ChangeDetectorRef)

4reactions
pkozlowski-opensourcecommented, Jul 4, 2022

This was just fixed via #46641 - the ComponentRef has a new method - setInput that correctly sets an input and makes sure that change detection is properly invoked. More details in #46641

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trigger update of component view from service - No Provider ...
ChangeDetectorRef is not option to use here. It is looking for changes in a given component and its children.
Read more >
Angular Change Detection - How Does It Really Work?
Angular can detect when component data changes, and then automatically re-render the view to reflect that change. But how can it do so...
Read more >
The Last Guide For Angular Change Detection You'll Ever Need
Change Detection: The process of updating the view (DOM) when the data has ... and therefore does not trigger change detection on an...
Read more >
Avoid common pitfalls when using OnPush change detection ...
When the OnPush change detection is declared as we see above, the change detection doesn't run automatically anymore.
Read more >
Using Angular Change Detection Effectively - Jagjeet Singh
Every component in angular has an associated View, This View ... In this strategy, angular triggers the change detection whenever any of theĀ ......
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