【问题标题】:How to add the class name from object iteration value?如何从对象迭代值中添加类名?
【发布时间】:2018-04-27 03:10:08
【问题描述】:

我正在尝试使用一些静态值从迭代器对象中添加类名。 我收到错误,如何从迭代器中添加类名?

<ul style="font-size: 20px">
    <li *ngFor="let link of socialLinks" [ngClass]="{ 'icon' + link.label}">{{link.label}}</li>
</ul>

我将icon 添加为静态,link.label 从对象添加。

【问题讨论】:

  • 如果你想在 ngClass 中添加一些条件,请使用大括号,否则使用不带括号的括号

标签: angular angular5


【解决方案1】:

不需要括号。

  <li *ngFor="let link of socialLinks" [ngClass]="'icon' + link.label">{{link.label}}</li>

【讨论】:

    【解决方案2】:

    ...添加更多解释。

    当您在任何属性的左侧使用属性绑定[] 时,您不需要在右侧使用{{}},因为属性绑定本身将右侧作为表达式进行计算。

    <li *ngFor="let link of socialLinks" [ngClass]="{ 'icon' + link.label}">{{link.label}}</li>
    

    所以在这种情况下,您要求 Angular 评估 "{ 'icon' + link.label}"link.label 已经是表达式。所以这就是为什么你只需要在这样的时间 -

    <li *ngFor="let link of socialLinks" [ngClass]="'icon' + link.label">{{link.label}}</li>
    

    【讨论】:

      猜你喜欢
      • 2019-12-04
      • 1970-01-01
      • 2012-05-23
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 2022-07-14
      相关资源
      最近更新 更多