javascript - 根据索引将元素添加到 ngfor dom
问题描述
我想基于索引渲染 DIV 元素,我不希望 DIV 渲染所有 ngfor“项目”。是否也可以在基于索引的 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
推荐阅读
- r - 闪亮应用程序中的选项卡名称之间没有空格
- ruby-on-rails - 如何从发布请求中重定向 Rails
- vue.js - VueJS + Vue.Draggable + Vuex Store + 计算变量
- html - 如何单击按钮并使用按钮所在项目的项目 ID 将其发送到另一个视图。Laravel - vue.js - 惯性 vue
- liferay - 我们如何以编程方式在 Liferay 中为用户和站点模板创建角色、权限?
- python - 如何使用 CPLEX Solver 定义具有 3d 成本矩阵的 CVRP 目标函数?
- python - 具有本地化日期时间索引的数据框:如何删除没有给定时间的日期
- c++ - 在哪里定义编译时常量?
- azure - Azure 存储模拟器的队列未加载
- c# - Ef 返回 null,但实体存在