簡體   English   中英

迭代相同組件angular2的多個@ViewChild實例

[英]Iterate through multiple @ViewChild instances of the same component angular2

我有一個Ionic 2應用程序,其中ParentComponent調用ChildComponent @ViewChild方法來啟動多個ChildComponents。 其中一個ChildComponents在視圖中使用不同的參數實例化兩次,如下所示:

<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();
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM