迭代通过相同组件的多个@ViewChild实例angular2
问题描述:
我有一个ParentComponent调用ChildComponent @ViewChild方法触发多个ChildComponents的Ionic 2应用程序。其中ChildComponents的获取在视图中使用不同的参数实例化两次,像这样的:迭代通过相同组件的多个@ViewChild实例angular2
<ChildComponent [startFrom]="0" [limitTo]="1"></ChildComponent>
<ChildComponent [startFrom]="1" [limitTo]="1"></ChildComponent>
离线/在线设备状态改变后,我打电话ChildComponent的方法来更新它返回的项目列表。
@ViewChild(ChildComponent) childComponent: ChildComponent;
ngOnInit(): void {
this.networkService.connectSubscription(() => {
this.childComponent.getItems();
});
}
这里的问题是this.childComponent
只得到的两个第一ChildComponent实例的保持。
有没有办法迭代同一个@ViewChild组件的多个实例,所以我可以做一些像this.childComponent1.getItems()
和this.childComponent2.getItems()
?
感谢您的帮助。
答
@ViewChildren(ChildComponent) childComponents: QueryList<ChildComponent>;
ngAfterViewInit(): void {
this.networkService.connectSubscription(() => {
this.childComponents.toArray();
});
}
绝对。令人讨厌的部分是我自己'this.childComponents',但不知道'toArray()'。谢谢你的帮助。 – YaroslavS120
我知道,这不容易发现。 –