[ngClass]与[class]绑定之间的区别 [英] Difference between [ngClass] vs [class] binding

查看:93
本文介绍了[ngClass]与[class]绑定之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段之间在Angular 2中有什么区别:

What is the difference in Angular 2 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.

这篇关于[ngClass]与[class]绑定之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆