在Angular2中有条件地分配类 [英] Assign classes conditionally in Angular2
问题描述
我试图根据从后端系统获得的值有条件地将一个类分配给div.
I'm trying to conditionally assign a class to a div based on the value I obtained from my back-end system.
假设存在一个对象'A'
,该对象的变量为status
,其值可能为status1
和status2
.
Say there is an object 'A'
which has a variable status
which possibly has values status1
and status2
.
我试图根据使用Angular 2的状态有条件地分配2个类(class1
和class2
).以下是我正在使用的条件.请为此建议一个可行的替代方案,
I'm trying to assign 2 classes (class1
and class2
) conditionally depending on the status using Angular 2. Following is the condition I'm using. Please suggest a working alternative for this,
<div ng-class="{status1 : 'class1', status2 : 'class2'}[A.status]">
...
</div>
推荐答案
您可以将对象绑定到[ngClass]
指令
You can bind your object to the [ngClass]
directive
<div [ngClass]="{'active': isActive, 'disabled': isDisabled}">
几乎与angular 1相同的语法.只需添加方括号
almost the same syntax as angular 1. just add the square brackets
这篇关于在Angular2中有条件地分配类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!