Angular [ngClass] vs [class [英] Angular [ngClass] vs [class
问题描述
以下片段之间的Angular有何不同:
What is the difference in Angular between the following snippets:
<div [class.extra-sparkle]="isDelightful">
<div [ngClass]="{'extra-sparkle': isDelightful}">
推荐答案
这是特殊的Angular绑定语法
This is special Angular binding syntax
<div [class.extra-sparkle]="isDelightful">
这是Angular编译器的一部分,您无法在此绑定样式后构建自定义绑定变体。唯一支持的是 [class.xxx] =...
, [style.xxx] =...
和 [attr.xxx] =...
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
是一个正常的Angular指令,就像你可以自己构建一样
ngClass
is a normal Angular directive like you can build it yourself
<div [ngClass]="{'extra-sparkle': isDelightful}">
ngClass
更强大。它允许您绑定类的字符串,字符串数组或对象中的对象。
ngClass
is more powerful. It allows you to bind a string of classes, an array of strings, or an object like in your example.
这篇关于Angular [ngClass] vs [class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!