Angular:使用来自Component的ng-For = obj创建的访问模板元素

问题描述 投票:0回答:1

指令模板 -

  <li *ngFor = "#el of dragZoneElems; #idx = index">
     <h4 [style.position]="'fixed'" [style.top.px]="idx* 30"   [style.margin-top] = "80.0" [style.z-index] = 100 [dragResponder] = "el">{{el.first}} {{el.last}}</h4>
  </li>

我需要使用指令类中的对象值来访问创建的h4元素。我该怎么做呢。

angular typescript
1个回答
1
投票

不确定你的“访问”是什么意思,但我认为这是你想要的:

 <li *ngFor = "#el of dragZoneElems; #idx = index">
     <!-- added: #h4 -->
     <h4 #h4 [style.position]="'fixed'" [style.top.px]="idx* 30"   [style.margin-top] = "80.0" [style.z-index] = 100 [dragResponder] = "el">{{el.first}} {{el.last}}</h4>
  </li>
class MyComponent {
  @ViewChildren('h4') h4s;

  ngAfterViewInit() {
    console.log(this.h4s.length);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.