How can I get children elements from QueryList in Angular 2?

前端 未结 2 1549
甜味超标
甜味超标 2021-02-04 01:23

I am newbie at Angular2. In my view I have few identical children that are generated in *ngFor.



        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 01:52

    If clients is set from an async call (for example to the server) then the elements don't exist in ngAfterViewInit() yet.

    You can use

    ngAfterViewInit() {
      this.processingItems.changes.subscribe(() => {
        this.processingItems.toArray().forEach(el => {
            console.log(el);
        });
      });
    }
    

    See also https://angular.io/api/core/QueryList#changes

提交回复
热议问题