Assign classes conditionally in Angular2

Ramana Sarva picture Ramana Sarva · Aug 28, 2016 · Viewed 46.8k times · Source

I'm trying to conditionally assign a class to a div based on the value I obtained from my back-end system.

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

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>

Answer

eltonkamami picture eltonkamami · Aug 28, 2016

You can bind your object to the [ngClass] directive

<div [ngClass]="{'active': isActive, 'disabled': isDisabled}">

almost the same syntax as angular 1. just add the square brackets