[ngClass] 与 [class] 绑定的区别 [英] Difference between [ngClass] vs [class] binding
问题描述
以下代码段在 Angular 2 中有什么区别:
<div [ngClass]="{'extra-sparkle': isDelightful}"> 解决方案 这是特殊的 Angular 绑定语法
这是 Angular 编译器的一部分,您无法按照此绑定样式构建自定义绑定变体.唯一支持的是 [class.xxx]=..."
、[style.xxx]=..."
和 [attr.xxx]=..."
ngClass
是一个普通的 Angular 指令,你可以自己构建它
<div [ngClass]={'extra-sparkle': isDelightful}">
ngClass
更强大.它允许您绑定类字符串、字符串数组或示例中的对象.
What is the difference in Angular 2 between the following snippets:
<div [class.extra-sparkle]="isDelightful">
<div [ngClass]="{'extra-sparkle': isDelightful}">
解决方案 This is special Angular binding syntax
<div [class.extra-sparkle]="isDelightful">
This is part of the Angular compiler and you can't build a custom binding variant following this binding style. The only supported are [class.xxx]="..."
, [style.xxx]="..."
, and [attr.xxx]="..."
ngClass
is a normal Angular directive like you can build it yourself
<div [ngClass]="{'extra-sparkle': isDelightful}">
ngClass
is more powerful. It allows you to bind a string of classes, an array of strings, or an object like in your example.
这篇关于[ngClass] 与 [class] 绑定的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文