最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Iterate through multiple @ViewChild instances of the same component angular2 - Stack Overflow

programmeradmin1浏览0评论

I have an Ionic 2 application with a ParentComponent calling ChildComponent @ViewChild method to fire up multiple ChildComponents. One of the ChildComponents get's instantiated twice in the view with different parameters like so:

<ChildComponent [startFrom]="0" [limitTo]="1"></ChildComponent>
<ChildComponent [startFrom]="1" [limitTo]="1"></ChildComponent>

After an offline/online device state change, I call ChildComponent's method to update a list of items it returns.

@ViewChild(ChildComponent) childComponent: ChildComponent;

ngOnInit(): void {
    thisworkService.connectSubscription(() => {
        this.childComponent.getItems();
    });
}

The issue here is this.childComponent only get's hold of the first ChildComponent instance of the two.

Is there a way to iterate through multiple instances of the same @ViewChild ponent so I could do something like this.childComponent1.getItems() and this.childComponent2.getItems()?

Thanks for your help.

I have an Ionic 2 application with a ParentComponent calling ChildComponent @ViewChild method to fire up multiple ChildComponents. One of the ChildComponents get's instantiated twice in the view with different parameters like so:

<ChildComponent [startFrom]="0" [limitTo]="1"></ChildComponent>
<ChildComponent [startFrom]="1" [limitTo]="1"></ChildComponent>

After an offline/online device state change, I call ChildComponent's method to update a list of items it returns.

@ViewChild(ChildComponent) childComponent: ChildComponent;

ngOnInit(): void {
    thisworkService.connectSubscription(() => {
        this.childComponent.getItems();
    });
}

The issue here is this.childComponent only get's hold of the first ChildComponent instance of the two.

Is there a way to iterate through multiple instances of the same @ViewChild ponent so I could do something like this.childComponent1.getItems() and this.childComponent2.getItems()?

Thanks for your help.

Share Improve this question asked Apr 10, 2017 at 8:57 CyberRobotCyberRobot 7449 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6
@ViewChildren(ChildComponent) childComponents: QueryList<ChildComponent>;
ngAfterViewInit(): void {
    thisworkService.connectSubscription(() => {
        this.childComponents.toArray();
    });
}
发布评论

评论列表(0)

  1. 暂无评论