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

前端 未结 2 1550
甜味超标
甜味超标 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

    0 讨论(0)
  • 2021-02-04 02:00

    I think you should be using the @ContentChildren attribute instead of the ViewChildren.

    @ContentChildren( OptionComponent )
    public Options: QueryList<OptionComponent>;
    
    0 讨论(0)
提交回复
热议问题