【问题标题】:Angular 2 - dynamic "for" attribute in ngFor [duplicate]Angular 2 - ngFor中的动态“for”属性[重复]
【发布时间】:2017-03-07 10:44:11
【问题描述】:

当我在标签中写动态“for”属性时,我得到了错误。

示例:

<div *ngFor="let question of questions; let i = index;">
        <input id="ques-{{i}}" type="radio" name="selected" [value]="question">
        <label for="ques-{{i}}">{{question.data}}</label>
</div>

错误:

未处理的 Promise 拒绝:模板解析错误:无法绑定到 'for' 因为它不是 'label' 的已知属性。

动态“id”属性工作正常,请帮我找到解决方案

【问题讨论】:

  • 您在ngFor 中缺少of,请使用此*ngFor="let question of questions;

标签: angular label ngfor


【解决方案1】:

你必须像这样访问标签的属性:[attr.for]="'ques-' + i"

【讨论】:

  • 嗨,谢谢它正在工作!但是为什么我不能访问“for”属性?为什么我必须使用 [attr]?
  • 因为有一些属性,如 'for' 'data' 'aria' 可能还有一些没有“角度”绑定,并且您不能在其中使用字符串插值跨度>
猜你喜欢
  • 1970-01-01
  • 2018-10-20
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
相关资源
最近更新 更多