[英]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.