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
ContentChildren query lags in change detection when wrapped by template #38976
Comments
Cannot think of any workarounds :( Maybe there's a way to manually kick it to refresh? At least with some private API, it's blocking me at the moment :( |
If you have access to the component the template is defined in (
I don't think this is the case. If I comment out the export class HelloComponent {
//...
constructor(ngZone: NgZone) {
// ngZone.runOutsideAngular(() => {
setInterval(() => {
console.log(this.children.length);
}, 1000);
// });
}
//...
} ...and type something in quickly, two seconds later after the observable emits I am still seeing Something is definitely up here. |
You are right about the zone. For some reason it did trigger it in my app where I originally stumbled upon it. Switching to |
Even EDIT: Ok, I've managed to find a workaround that unblocked me. I've added a directive to template and injected |
Adding: ngDoCheck() {
this._cref.detectChanges();
} to ngDoCheck() {
const isDirty = (this as any).__ngContext__[2] & 0b000001000000;
if (isDirty) {
this._cref.detectChanges();
}
} But if we're talking about private APIs...then there may be a more performant solution. Here is a modified StackBlitz that adds the same query for The |
@atscott is this potentially related transplanted view change detection? |
@jelbourn Yea, this is related to transplanted views. Ironically, there was a short period of time that this actually worked (change the version to Issue summary:
My guess is that the implementation of content queries didn't consider that the ng-content might have transplanted views. I would also guess that fixing this would end up being a breaking change. The content query results should be available before executing the template of the insertion component or we could otherwise run into Just a small note that the behavior is quite a departure from what's expected. The act of executing the Simpler repro without the intervals: https://stackblitz.com/edit/angular-content-children-query-bug-vjaxd3?file=src%2Fapp%2Fapp.component.html |
🐞 bug report
Affected Package
The issue is caused by package @angular/core probablyIs this a regression?
No, the issue was present at least since version 7Description
When your component has
ng-template
in its content for later instantiation and inside that template there's a list of items — you can query those children withContentChildren
once you instantiate the template. However change detection in them lags a cycle.🔬 Minimal Reproduction
https://stackblitz.com/edit/angular-content-children-query-bug
🌍 Your Environment
Angular Version:
Anything else relevant?
In the reproduction Stackblitz, type a short string into input.
This string is then broken into chars and spread into a list of DIVs with a 2 seconds delay.
You can even see that "content checked" is printed to console when the list updates.
However the
QueryList
of children is not refreshed.Also note that I had to wrap
setInterval
intorunOutsideAngular
.Otherwise it would create a tick that refreshes the query, even though both components are
OnPush
.The text was updated successfully, but these errors were encountered: