简体   繁体   English

在 Angular 中的 ngClass 中绑定自定义 class

[英]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.我正在尝试在 Angular ngClass 中绑定一个 class 名称,以便用户能够将他喜欢的任何 class 名称设置到该元素。

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.我的 js 文件中有以下内容,如果设置了 this.customClass,我想将此 class 名称分配给 div 元素。

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

HTML 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 https://angular.io/api/common/NgClass

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

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

No need of using ngClass .无需使用ngClass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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