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.

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:closed
  • Created 4 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Andy1605commented, Apr 25, 2019

I added the link to a StackBlitz project

1reaction
cexbrayatcommented, Nov 29, 2019

@Aleksandyr Awesome, thank you for checking!

Read more comments on GitHub >

github_iconTop 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 >

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