Javascript Angular 2 通过 [class.className] 绑定添加多个类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43180436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:44:18  来源:igfitidea点击:

Angular 2 add multiple classes via [class.className] binding

javascriptangularbinding

提问by Ajey

While adding a single class works great in this way -

虽然以这种方式添加单个类效果很好 -

[class.loading-state]="loading"

[class.loading-state]="loading"

But how do I add multiple classes Ex if loadingis trueadd class - "loading-state" & "my-class"

但是如果loadingtrue添加类,我如何添加多个类 -"loading-state" & "my-class"

How do I get it done via the [class] binding

我如何通过 [class] binding

回答by Vivek Doshi

You can do this by simply using ngClass:

您可以通过简单地使用ngClass

Here first,second and third are the name of the classes.

这里第一、第二和第三是类的名称。

And instead of true/false , you can directly put your conditions over there

而不是 true/false ,你可以直接把你的条件放在那里

 <div [ngClass]="{'first': true, 'second': true, 'third': false}">...</div>

In your case

在你的情况下

 <div [ngClass]="{'loading-state': loading, 'my-class': loading }">...</div>

Or Shorter Veriosn (as @matko.kvesic commented)

或者更短的 Veriosn(正如@matko.kvesic 评论的那样)

<div [ngClass]="{'loading-state my-class': loading}">...</div>