CSS 类绑定
借助 CSS 类绑定,可以从元素的 class
attribute 上添加和移除 CSS 类名。
CSS 类绑定绑定的语法与属性绑定类似。 但方括号中的部分不是元素的属性名,而是由class
前缀,一个点 (.
)和 CSS 类的名字组成, 其中后两部分是可选的。形如:[class.class-name]
。
下列例子示范了如何通过 CSS 类绑定来添加和移除应用的 "special" 类。不用绑定直接设置 attribute 时是这样的:
SRC /应用程序/ app.component.html
content_copy<!-- standard class attribute setting -->
<div class="bad curly special">Bad curly special</div>
可以把它改写为绑定到所需 CSS 类名的绑定;这是一个或者全有或者全无的替换型绑定。 (译注:即当 badCurly 有值时 class 这个 attribute 设置的内容会被完全覆盖)
您可以使用绑定到所需类名的字符串来替换它; 这是一个全有或全无的替代绑定。
SRC /应用程序/ app.component.html
content_copy<!-- reset/override all class names with a binding -->
<div class="bad curly special"
[class]="badCurly">Bad curly</div>
最后,可以绑定到特定的类名。 当模板表达式的求值结果是真值时,Angular 会添加这个类,反之则移除它。
SRC /应用程序/ app.component.html
content_copy<!-- toggle the "special" class on/off with a property -->
<div [class.special]="isSpecial">The class binding is special</div>
<!-- binding to `class.special` trumps the class attribute -->
<div class="special"
[class.special]="!isSpecial">This one is not so special</div>
虽然这是切换单一类名的好办法,但人们通常更喜欢使用 NgClass 指令 来同时管理多个类名。
NgClass指令
NgClass
你经常用动态添加或删除 CSS 类的方式来控制元素如何显示。 通过绑定到 NgClass
,可以同时添加或移除多个类。
CSS 类绑定 是添加或删除单个类的最佳途径。
SRC /应用程序/ app.component.html
content_copy<!-- toggle the "special" class on/off with a property -->
<div [class.special]="isSpecial">The class binding is special</div>
当想要同时添加或移除多个 CSS 类时,NgClass
指令可能是更好的选择。
试试把 ngClass
绑定到一个 key:value 形式的控制对象。这个对象中的每个 key 都是一个 CSS 类名,如果它的 value 是 true
,这个类就会被加上,否则就会被移除。
组件方法 setCurrentClasses
可以把组件的属性 currentClasses
设置为一个对象,它将会根据三个其它组件的状态为 true
或 false
而添加或移除三个类。
SRC /应用程序/ app.component.ts
content_copycurrentClasses: {};
setCurrentClasses() {
// CSS classes: added/removed per current state of component properties
this.currentClasses = {
'saveable': this.canSave,
'modified': !this.isUnchanged,
'special': this.isSpecial
};
}
把 NgClass
属性绑定到 currentClasses
,根据它来设置此元素的 CSS 类:
SRC /应用程序/ app.component.html
content_copy<div [ngClass]="currentClasses">This div is initially saveable, unchanged, and special</div>
你既可以在初始化时调用 setCurrentClassess()
,也可以在所依赖的属性变化时调用。
有些样式绑定中的样式带有单位。在这里,以根据条件用 “em” 和 “%” 来设置字体大小的单位。
SRC /应用程序/ app.component.html
content_copy<button [style.font-size.em]="isSpecial ? 3 : 1" >Big</button>
<button [style.font-size.%]="!isSpecial ? 150 : 50" >Small</button>
NgStyle
你可以根据组件的状态动态设置内联样式。 NgStyle
绑定可以同时设置多个内联样式。
样式绑定是设置单一样式值的简单方式。
SRC /应用程序/ app.component.html
content_copy<div [style.font-size]="isSpecial ? 'x-large' : 'smaller'" >
This div is x-large or smaller.
</div>
如果要同时设置多个内联样式,NgStyle
指令可能是更好的选择。
NgStyle
需要绑定到一个 key:value 控制对象。 对象的每个 key 是样式名,它的 value 是能用于这个样式的任何值。
来看看组件的 setCurrentStyles
方法,它会根据另外三个属性的状态把组件的 currentStyles
属性设置为一个定义了三个样式的对象:
SRC /应用程序/ app.component.ts
content_copycurrentStyles: {};
setCurrentStyles() {
// CSS styles: set per current state of component properties
this.currentStyles = {
'font-style': this.canSave ? 'italic' : 'normal',
'font-weight': !this.isUnchanged ? 'bold' : 'normal',
'font-size': this.isSpecial ? '24px' : '12px'
};
}
把 NgStyle
属性绑定到 currentStyles
,以据此设置此元素的样式:
SRC /应用程序/ app.component.html
content_copy<div [ngStyle]="currentStyles">
This div is initially italic, normal weight, and extra large (24px).
</div>
你既可以在初始化时调用 setCurrentStyles()
,也可以在所依赖的属性变化时调用。