【问题标题】:Angular2 *ngFor setting index as attribute inside same tag [duplicate]Angular2 *ngFor将索引设置为同一标签内的属性[重复]
【发布时间】:2016-06-10 09:49:54
【问题描述】:

我正在尝试创建一个引导轮播。所以我使用 *ngFor 添加元素和轮播指示器。(指示当前位置的小圆圈)

<li data-target="#myCarousel" *ngFor="#item of items; #idx = index"  data-slide-to="idx" [class.active]="idx === 0" >

我通过 [class.active]="idx​​ === 0" 设置活动类条目,它工作正常。 但是当我尝试设置 data-slide-to="idx​​" 时,结果不是想要的索引为数字,而是字符串“idx”。

知道如何分配索引值吗?

【问题讨论】:

    标签: indexing angular ngfor


    【解决方案1】:

    你有两个选择:

    1.直接绑定到属性

    [attr.data-slide-to]="idx"
    

    2。使用字符串插值

    attr.data-slide-to="{{idx}}"
    

    【讨论】:

      【解决方案2】:

      绑定需要[]{{}}。绑定属性需要attr.前缀

      [attr.data-slide-to]="idx"
      

      【讨论】:

        猜你喜欢
        • 2016-05-26
        • 2016-11-25
        • 2016-05-26
        • 1970-01-01
        • 1970-01-01
        • 2016-10-25
        • 2017-08-22
        • 2021-10-27
        相关资源
        最近更新 更多