conditional css class angular code example

Example 1: conditional style angular

<div [ngStyle]="{'color':employee.country === 'India' ? 'orange' : 'red' }"></<div>

Example 2: conditionally assign a css class angular 9

<div [ngClass]="{'message': info.priority < 10,
                 'warn': info.priority > 10 && info.priority < 20, 
                 'error': info.priority > 30}">

{{info.text}}

</div>

Example 3: conditional classes angular

<div 
  [className]="isActive ? 'active' : 'inactive'">
</div>

Example 4: conditional inline style angular

// ngStyle (conditional Style)
<div *ngFor='let [data] of [dataArray]; let i=index;'>
  <h2 [style.background]="i>1? 'green': 'red'"> {{i}} {{data.title}} </h2>

  </div> 

//if the index of the element is 1 or 0, the background will be red, if it is over 1 the background will be green

Tags:

Html Example