Angular 2+: Get child element of @ViewChild()

前端 未结 4 1134
[愿得一人]
[愿得一人] 2021-01-17 19:04

How can I access the child elements (here: ) of @ViewChild() in Angular 2+ without explicit declaration?

In template.htm

4条回答
  •  天涯浪人
    2021-01-17 19:53

    use ViewChildren instead which gets all elements with same tag.

    @ViewChildren('parent') parents: QueryList;
    

    To convert those elements to array:

    const arr = this.parent.toArray();
    

    Choose first element:

    const el = arr[0]
    

    Access child html of first element: const innerHtml = el.nativeElement.innerHtml;

提交回复
热议问题