首页 文章

Angular2 * ng将索引设置为同一标签内的属性[重复]

提问于
浏览
2

这个问题在这里已有答案:

我试图创建一个自举轮播 . 所以我使用* 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” .

知道如何分配索引值吗?

2 回答

  • 5

    你有两个选择:

    1. Bind directly to the attribute

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

    2. Use string interpolation

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

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

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

相关问题