简体   繁体   English

如何在循环内的 Angular 模板上的 div 中动态命名引用

[英]How to name dynamically a reference in a div on Angular template inside a loop

I'm developing a project using Angular 8.3.我正在使用 Angular 8.3 开发一个项目。

I have a loop (using *ngFor) and I need that every div that I add has an unique reference name created dinamically).我有一个循环(使用 *ngFor),我需要我添加的每个 div 都有一个以动态方式创建的唯一引用名称)。

This is my example code:这是我的示例代码:

<div *ngFor="let item of list; let i = index;">
    <div
        cdkDropList
        #done0="cdkDropList"></div>
</div>

Below I have another drag and drop zone that must receive these elements:下面我有另一个必须接收这些元素的拖放区域:

<div
          cdkDropList
          #donesList="cdkDropList"
          [cdkDropListData]="DonesList"
          [cdkDropListConnectedTo]="[done0, done1]"
          class="movie-list"
          (cdkDropListDropped)="onDrop($event)">
            <div *ngFor="let itemList of DonesList" cdkDrag>{{itemList}}</div>
          </div>

I need to change dynamically #done0, eg (#done0, #done1, #done2, #done3, etc).我需要动态更改#done0,例如(#done0、#done1、#done2、#done3 等)。

How can I do it?我该怎么做? I have tryed using #done{{i}}} but it doesn't work.我尝试过使用#done{{i}}},但它不起作用。

I don't know if I can use trackBy in this case and how to apply it.我不知道在这种情况下我是否可以使用 trackBy 以及如何应用它。

Many thanks,非常感谢,

Reference a container引用容器

<div *ngFor="let item of list; let i = index;" #doneContainer>
      <div cdkDropList</div>
</div>

and in.ts you have different references in a NodeList和 in.ts 你在 NodeList 中有不同的引用

const list = this.doneContainer.childNodes 

or in more Angular way, which is better as it will update when necessary或更多 Angular 方式,更好,因为它会在必要时更新

  @ViewChildren('doneContainer') list: QueryList<any>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 function 中动态设置模板引用 - How to set template reference dynamically inside function 如何在速度模板的for循环内为div生成唯一的类名称? - How can I generate unique class name for div inside a for loop in velocity template? 如何在循环内动态创建Angular手表 - How to dynamically create Angular watches inside a loop 如何在角度模板中动态访问对象的键 - How to access keys of an object dynamically inside angular template 在Angular 6中使用ngFor循环动态创建输入元素数组并基于模板引用变量添加动态验证 - use ngFor loop in Angular 6 to dynamically create array of input elements and add dynamical validation based on template reference variables 如何打开/关闭点击div生成内部循环与角度js? - How to open/close clicked div generating inside loop with angular js? 如何在Angularjs中动态命名div? - How to dynamically name the div in Angularjs? 如何在Angular 6中基于ng-template内的id引用元素? - How to reference an element based on id which is inside an ng-template in Angular 6? 角度模板参考:将一个 div 的高度绑定到另一个的高度 - Angular Template Reference : Bind height of one div to height of another 您如何在使用jQuery的for循环中动态创建的div名称后面追加? - How do you append to a div name that is being dynamically created in a for loop with jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM