【问题标题】:Add and remove classes Angular without controller在没有控制器的情况下添加和删除类 Angular
【发布时间】:2019-10-31 05:42:10
【问题描述】:

我刚刚开始了一个新的 Angular 项目,我学到了很多 *ngIf。我想使用 *ngIf 来从 div 中添加/删除一个类

点击前

<div class="done">Foo</div>

点击后

<div class="notdone">Foo</div>

【问题讨论】:

  • 使用ngClass 而不是ngIf 从这里阅读https://angular.io/api/common/NgClass

标签: angular


【解决方案1】:

你有不同的方法来解决这个问题。

<div [ngClass]="{'done': conditionToDone, 'notdone': !conditionToDone}">Foo</div>

donenotdone 是通过条件添加的。

【讨论】:

  • 非常感谢!它解决了我的问题。不得不说,你在'{'之后和'}'之前离开了'"',报错了,不过很快就解决了!
  • 编辑了我的答案。乐于助人。
  • 另一个建议:[ngClass]="condition ? 'done' : 'notdone'".
猜你喜欢
  • 2014-12-31
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多