Search code examples
angularangular-ng-class

How to add condition and function both in ngClass?


I need to use both function and condition in ngClass like below:

<div [ngClass] = "i==1 ? 'active':'inactive',getclass()">
</div

TS

getclass(){
return 'myclassname'
}

Solution

  • You can use function as well. You just need to pass as an array like below,

    <div [ngClass]="[i === 1 ? 'active': 'inactive', getClass()]"> </div

    You can find other ways in this link https://angular.io/api/common/NgClass#description