简体   繁体   中英

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>

To bind class

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

please check the below

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

您不能在ngClass中使用字符串插值。请在下面使用它可能会有所帮助。

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

No need of using ngClass .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM