【问题标题】:add element to ngfor dom based on index根据索引将元素添加到 ngfor dom
【发布时间】:2020-05-01 02:04:11
【问题描述】:

我想根据索引渲染 DIV 元素,我不希望 DIV 渲染所有 ngfor“项目”。是否也可以在基于索引的 DIV 中拥有动态内容。感谢你的帮助。谢谢你。

<ul>
 <li *ngFor="let item of items; let i =index">
    <i (click)="add(i)">
    <div *ngIf="i">

    </div>
 </li>
</ul>

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    要根据索引在 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 属性将值与{{ }} 绑定。

    【讨论】:

    • 谢谢马丁。
    猜你喜欢
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 2016-05-17
    • 2018-01-21
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多