'Binding custom class inside ngClass in Angular

I am trying to bind a class name inside Angular ngClass so that the user will be able to set any class name he likes to the element.

I have the following in my js file and would like to assign this class name to the div element if this.customClass is set.

this.customClass= "list-custom-class";

HTML

<div class="row" [ngClass]="'{{customClass}}'}"></div>


Solution 1:[1]

To bind class

<div class="row" [ngClass]="customClass"></div>

please check the below

https://angular.io/api/common/NgClass

Solution 2:[2]

You can't use string interpolation in ngClass.Please use below it may help.

[ngClass]="[customClass]"

Solution 3:[3]

<div class="row {{customClass}}"></div>

No need of using ngClass.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yasser Mas
Solution 2 Rushi Patel
Solution 3 Xavier Guihot