angular - *ngFor 带索引

标签 angular

我有 Angular 4 应用程序,我想在其中从 *ngFor 获取列的索引 并将其附加到 HTML 元素,我已经尝试了 angular.io 中的示例但它给了我一个错误

代码:

<li *ngFor="let col of Descriptive_FieldsMap;let i = index;">
  <a class="ShowHideColumns" data-columnindex="{{i + 1}}">
      {{col.FieldName}}
     <i id="data-table-col-{{i + 1}}" class="fa fa-minus-square pull-right">
     </i>
  </a>
</li>

错误:

Error: Template parse errors:↵Can't bind to 'columnindex' since it isn't a known property of 'a'. ("tive_FieldsMap;let i = index;">
↵

最佳答案

data-columnindex不是 <a> 的原生属性标记你必须采取另一种方法。与 attr 绑定(bind)指令:

<a class="ShowHideColumns" [attr.data-columnindex]="i + 1">

https://angular.io/docs/ts/latest/guide/template-syntax.html#!#other-bindings

关于angular - *ngFor 带索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44296980/

相关文章:

javascript - 在 Angular 中添加记录后附加 HTML

angular - 如何在 angular2 GA 中使用 Http 提供程序?

angular - 从 Angular4 上的 md-dialog 获取数据

angular - 在 Angular 中找不到模块文件保护程序

html - 用什么代替::ng-deep

angular - 如何隐藏 Electron 默认菜单

Angular 2 [disabled] 仅适用于某些控件

Angular 2 Material 日期选择器ngModel不显示值

angular - 为什么我需要 *public-api.ts* 和 *exports*

Angular 错误 TS2322 : Type 'Promise<Dish[]>