javascript - 遍历同一组件angular2的多个@ViewChild实例

标签 javascript angular ionic2 angular2-components

我有一个 Ionic 2 应用程序,它的 ParentComponent 调用 ChildComponent @ViewChild 方法来启动多个 ChildComponent。其中一个 ChildComponents get 在 View 中使用不同的参数实例化了两次,如下所示:

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

关于javascript - 遍历同一组件angular2的多个@ViewChild实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43318838/

相关文章:

android - Ionic 2 推送通知错误

javascript - 随着背景颜色的变化更改按钮颜色?

javascript - 数组不会在 javascript 中使用 array.from 生成

带有 Promise 的 Javascript 递归函数

angular - 在 Ionic 4 应用程序中使用 @ionic-native/http

angular - 创建了新组件,但我在模块中收到有关提供程序的错误

javascript - 我可以使用 indexOf(input) 吗?

javascript - 在 Angular 中访问子项

facebook - 模块没有导出成员 'IonicNativePlugin' ,用于 facebook 的 Ionic2

javascript - ionic 2 + Angular 2 : auto scroll to bottom of list/page/chat