1<div [ngStyle]="{'background-color':person.country === 'UK' ? 'green' : 'red' }"></<div>
1//Type one
2[class.my_class] = "step === 'step1'"
3
4//Type two
5[ngClass]="{'my_class': step === 'step1'}"
6and multiple option:
7[ngClass]="{'my_class': step === 'step1', 'my_class2' : step === 'step2' }"
8
9//Type three
10[ngClass]="{1 : 'my_class1', 2 : 'my_class2', 3 : 'my_class4'}[step]"
11
12//Type four
13[ngClass]="step == 'step1' ? 'my_class1' : 'my_class2'"
1Angular version 2+ provides several ways to add classes conditionally:
2
3type one
4
5[class.my_class] = "step === 'step1'"
6type two
7
8[ngClass]="{'my_class': step === 'step1'}"
9and multiple option:
10
11[ngClass]="{'my_class': step === 'step1', 'my_class2' : step === 'step2' }"
12type three
13
14[ngClass]="{1 : 'my_class1', 2 : 'my_class2', 3 : 'my_class4'}[step]"
15type four
16
17[ngClass]="step == 'step1' ? 'my_class1' : 'my_class2'"
1
2 content_copy
3
4 <some-element [ngClass]="'first second'">...</some-element>
5
6<some-element [ngClass]="['first', 'second']">...</some-element>
7
8<some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>
9
10<some-element [ngClass]="stringExp|arrayExp|objExp">...</some-element>
11
12<some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>
13