在Angular2中有条件地分配类 [英] Assign classes conditionally in Angular2

查看:88
本文介绍了在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,其值可能为status1status2.

Say there is an object 'A' which has a variable status which possibly has values status1 and status2.

我试图根据使用Angular 2的状态有条件地分配2个类(class1class2).以下是我正在使用的条件.请为此建议一个可行的替代方案,

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆