简体   繁体   English

Angular QueryList更改

[英]Angular QueryList changes

I have a component "ProductList" that contains "Product" components. 我有一个包含“产品”组件的组件“ ProductList”。 When the list changes I want to remove old eventlistener and add a new one........... 当列表更改时,我想删除旧的事件监听器并添加一个新的事件监听器。

@ViewChildren(ProductComponent) products: QueryList<any>;

Then I subscribe to the QueryList changes in ngAfterViewInit hook 然后我订阅ngAfterViewInit钩子中的QueryList更改

this.products.changes.subscribe(products => {
 console.log(‘changes’, this.products.length);
});

The problem is that it fires twice when the querylist is updated.. once before the list is updated and once after.. ?? 问题在于,当查询列表更新时,它会触发两次。在列表更新之前,它会触发一次。

You do not need to remove listeners and add new ones when the list changes. 列表更改时,您无需删除侦听器并添加新的侦听器。 QueryList is updated as the new elements arrive. 随着新元素的到来,QueryList也会更新。

See what the docs have to say about it: 看看文档对此有何评论:

An unmodifiable list of items that Angular keeps up to date when the state of the application changes. 当应用程序状态更改时,Angular会保持最新的不可更改的项目列表。

Documentation 文献资料

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM