我想根据索引渲染 DIV 元素,但不希望 DIV 渲染所有 ngfor“items”。是否也可以在基于索引的 DIV 中包含动态内容。感谢你的帮助。谢谢。
<ul>
<li *ngFor="let item of items; let i =index">
<i (click)="add(i)">
<div *ngIf="i">
</div>
</li>
</ul>
最佳答案
要在 div 中基于索引添加动态内容,您只需使用 *ngIf,如您在示例中提供的那样:
<ul>
<li *ngFor="let item of items; let i = index">
<i (click)="add(i)">
<div *ngIf="i === 0">
Content for the first index
</div>
</li>
</ul>
div 只会出现在 DOM 中的第一个索引中。
您还可以根据作为参数提供的 i
属性使用 {{ }}
绑定(bind)值。
关于javascript - 根据索引向 ngfor dom 添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61529730/