angular @ViewChild和@ViewChildren的使用

@ViewChild和@ViewChildren都是模板变量,前者是单一的,后者是以数组的形式存储

用法

1、在需要使用的标签上用#变量名标识一个标签

      <div id="report" #report>
        <div #reportContainer id="item">
        </div>
      </div>
2、在TS文件中定义
 @ViewChild('report') report:any | undefined
3、使用
   获取子元素的个数 this.report.valueOf().nativeElement.childNodes.length
 
  获取子元素中的数据 this.report.valueOf().nativeElement.childNodes[1].__ngContext__[30].title
 
posted @ 2021-11-03 21:35  影随风舞  阅读(450)  评论(0)    收藏  举报