-
Notifications
You must be signed in to change notification settings - Fork 26.2k
OnPush change detection doesn't work for outputs emitted from lifecycle callbacks #24965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@sebek64 ... do you have any problems with creating an online reproduction demo using https://stackblitz.com because it is much easier for everybody to look at it than to clone a repository, run CLI, ... and so on ... ? |
@mlc-mlapis Well, actually no, just laziness :). So I'll prepare it. Initially, a few weeks ago, I started by creating an obvious demo online and failed, because it always worked as expected. So this time, I used more convenient tools for me. So I try to copy everything into an online version and post a link. I suspect some race condition according to the behavior in the real-world project, so it may well be irreproducible in web-based tool. |
Here is a web-based repro: https://stackblitz.com/edit/angular-gitter-egxquq |
@mlc-mlapis With stackblitz you can load the code direktly from GitHub. |
@manklu ... yep, thanks, I forgot about this feature. 👍 |
Yes, there is a problem with events during change detection. As workaround you can use something like this: Promise.resolve().then(() => this.valueChange.emit("emitted value from ngOnInit")); Better than calling detectChanges() directly. |
This should raise an |
So if I understand it correctly, the described scenario is not going to work, because the change detection mechanism is not designed to support it. Based on the @manklu comment, I found another possible workaround - use "async = true" for EventEmitter, i.e., new EventEmitter<...>(true). |
If most events are emitted during change detection, this is the best solution. |
As @trotyl mentioned, this should result in |
Closing as duplicate of #45612 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
This issue is related to the closed issue #24690 for which I managed to create a simple repro case. Complete description follows.
When a component with OnPush change detection subscribes an output of a subcomponent, it works pretty well, unless this subcomponent emits values from the initialization lifecycle callbacks (ngOnInit, ngAfterViewInit). Then, no change detection is performed, and even calling changeDetectorRef.markForCheck() doesn't help. Only calling detectChanges() manually works.
Quite surprisingly, when the value is emitted from ngOnInit, it is shown in the interpolation strings, but not passed to subcomponents. On the other hand, ngAfterViewInit value is not used at all.
Expected behavior
It should work out of the box, without the need to work with change detector manually. If this is not supported for some architectural reason, the documentation should contain a big warning about it.
Minimal reproduction of the problem with instructions
I've prepared a git repo here: https://github.com/sebek64/angular-issue-repro
The initial commit is a plain angular-cli project. The next commit adds the repro code. Run via "yarn start", open the browser, and you should see output like this:
When the button is clicked, the output changes to
What is the motivation / use case for changing the behavior?
Environment
The text was updated successfully, but these errors were encountered: