Angular 4: setting the local variable # dynamically

后端 未结 3 1352
情书的邮戳
情书的邮戳 2020-12-09 11:24

Is there a way to dynamically set the #id, the HTML attribute that angular uses to construct the @ViewChild element reference?

Specific need: i have the following te

相关标签:
3条回答
  • 2020-12-09 11:59

    You can use like this code:

    <ul>
        <li *ngFor="let item of items;let i = index;" #itemRef{{i}}>{{item.xyz}}</li>
    </ul>
    

    Look this question: enter link description here

    0 讨论(0)
  • 2020-12-09 12:05

    You can use the index for it.

     <ul>
    <li *ngFor="let link of links; let i=index;">
    {{link.title}}{{i}} 
    </li>
    </ul>
    
    0 讨论(0)
  • 2020-12-09 12:16

    Despite the seeming resemblance between regular variables and #, multiple elements can be assigned to single local template reference variable:

    <ul>
       <li *ngFor="let link of links" #linkRef></li>
    </ul>
    

    Which can be obtained with:

    @ViewChildren('linkRef') linkRefs;
    
    0 讨论(0)
提交回复
热议问题