0

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>
Sha
  • 1,826
  • 5
  • 29
  • 53

4 Answers4

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

No need of using ngClass.

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
Prashant Singh
  • 611
  • 1
  • 5
  • 14
0

To bind class

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

please check the below

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

Yasser Mas
  • 1,652
  • 1
  • 10
  • 14
0

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

[ngClass]="[customClass]"
Rushi Patel
  • 561
  • 4
  • 14
0

You can directly use it as class using string interpolation. Need not use ngClass to bind dynamic class through a variable.ngClass can be used in situations where you want to add class based on some conditions etc.,Here is the output

<span class="align-middle {{sub.sub2_sec_class}}" 
      [ngClass]="sub.isCurrent?'tab-head-icon-selected':'tab-head-icon'"> 
      {{sub.iconName}}
</span>

Here 'sub.sub2_sec_class' is a variable which dynamically populated for every ngFor loop iteration