QueryList.changes is triggered for any view changes (even siblings)
See original GitHub issue🐞 bug report
Affected Package
@angular/core@7.2.14
Is this a regression?
It worked as expected in Version 2.4.9
Description
A QueryList (bound via ViewChildren decorator) is always emitting the changes event even though no of its observed elements have changed. In fact it gets emitted for any view changes within the same view.
🔬 Minimal Reproduction
A minimal example is the following component:
@Component({
selector: 'querylist-sandbox',
template: `
<button (click)="showAddButton = !showAddButton">TOGGLE INPUT</button>
<button (click)="itemList.push( itemList.length )" *ngIf="showAddButton">ADD</button>
<div #items *ngFor="let item of itemList">{{item}}</div>`
})
export class QueryListSandbox {
@ViewChildren('items')
private items: QueryList<ElementRef>;
private showAddButton = true;
private itemList = [ 0, 1, 2 ];
public constructor() {
}
public ngAfterViewInit() {
this.items.changes.subscribe( ql => {
console.log( ql, 'CHANGES!' );
});
}
}
On StackBlitz: https://stackblitz.com/edit/angular-ru9m6u
🔥 Exception or Error
The console log is also made when the TOGGLE button is pressed. I only expect the event to be triggered when i press the ADD button, though.
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (4 by maintainers)
Top Results From Across the Web
ViewChildren-QueryList changes subscription is called for any ...
My view children is querying a list of item ElementRefs, however, even if there is no change in the number of items, the...
Read more >QueryList - Angular
Triggers a change event by emitting on the changes EventEmitter . notifyOnChanges(): void. Parameters.
Read more >Testing Components with children - Testing Angular
The spec finds the app-counter element and triggers the countChange event handler. The second triggerEventHandler parameter, 5 , is not an event ......
Read more >Heading Off 11 Common Angular Application Design, Best ...
As a user change could happen through a modal window, it's a prime example of a change that can occur without any routing...
Read more >Change Detection in Angular: Everything You Need to Know
Properties of elements in a View can change, but the structure (number ... When an asynchronous event takes place, Angular triggers change ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I added the link to a StackBlitz project
@Aleksandyr Awesome, thank you for checking!