首页 > 解决方案 > 在角度,如何使用方括号添加一个类?

问题描述

是否可以创建类似下面的内容?

<div *ngFor="let post of posts">
  <p class="quote {{post.color}}-quote">
    {{ post.content }}
    <span class="author">Anatoly Weinstein, Founder of Theora</span>
  </p>
</div>

重要线路:class="quote {{post.color}}-quote"

我见过类似的问题,但我不想使用布尔值并对每个元素进行硬编码。

标签: cssangularangular-template

解决方案


使用带括号的模板绑定没有特殊原因,这可以通过ngClass指令来完成:

<p class="quote" [ngClass]="[post.color+'-quote']">

推荐阅读