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.

`ChangeDetectorRef.markForCheck()` in the `ngAfterContentInit` of a nested component is ignored

See original GitHub issue

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

core

Is this a regression?

No

Description

Suppose you have a component Root, which uses component Child in its view. Component Child implements the ngAfterContentInit hook. If this hook somehow triggers markForCheck() in the ChangeDetectorRef of the Root component, this call is ignored.

I don’t know if this is actually a bug. The minimal StackBlitz uses Angular in a very weird way. That’s why I created a second StackBlitz which shows a potential use case where it causes problems, using the async pipe and a service. You can easily run into this problem if you use rxjs to bidirectionally communicate between components.

Both examples show that calling markForCheck in the button click trigger a content change because the actual markForCheck got ignored.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-sbhh57 (minimal)

https://stackblitz.com/edit/angular-ivy-jkqseu (showing use case)

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in

Angular 12.2.9
Linux (WSL)
Node 12.18.3

Anything else?

You’re awesome!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, Oct 20, 2021

This appears to be due to a data flow issue that would result in ExpressionChangedAfterItHasBeenCheckedError. The data model is currently dependent on the render result of the UI, but then that updated data model (concretely, its count) is also rendered into the UI. That’s invalid, as it means that two change detection cycles are required to correctly update the DOM.

The ExpressionChangedAfterItHasBeenCheckedError is not actually reported due to a known bug with OnPush components. A component that is OnPush marks itself as non-dirty before the check no changes phase, causing the check no changes cycle to also skip visiting that component.

If you remove all OnPush usages you will see the ExpressionChangedAfterItHasBeenCheckedError being reported.

0reactions
angular-automatic-lock-bot[bot]commented, Nov 22, 2021

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

Expression ___ has changed after it was checked
This is done using the detectChanges() method of the ChangeDetectorRef ... or its markForCheck() method, which also makes any parent components update.
Read more >
Change Detection not picking up deeply nested route ... - GitHub
If I change my component to call changeDetectorRef.detectChanges() when it receives new data instead of changeDetectorRef.markForCheck() then ...
Read more >
Everything you need to know about change detection in Angular
Changing the structure of Elements can only be done by inserting, moving or removing nested Views via a ViewContainerRef. Each View can contain...
Read more >
ChangeDetectorRef - Angular
The following examples demonstrate how to modify default change-detection behavior to perform explicit detection when needed. Use markForCheck() with CheckOnce ...
Read more >
angular/core/core.d.ts - UNPKG
696, * ### Use `markForCheck()` with `CheckOnce` strategy ... 2298, * When present, this directive/component is ignored by the AOT compiler.
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