NgClass

Adds and removes CSS classes on an HTML element.

从 HTML 元素上添加和移除 CSS 类。

@Directive({ selector: '[ngClass]' }) class NgClass implements DoCheck { set klass: string set ngClass: string | string[] | Set<string> | {...} ngDoCheck(): void }

选择器

[ngClass]

输入参数

class 绑定到 NgClass.klass
ngClass 绑定到 NgClass.ngClass

说明

The CSS classes are updated as follows, depending on the type of the expression evaluation:

CSS 类会根据表达式求值结果进行更新,更新逻辑取决于结果的类型:

  • string - the CSS classes listed in the string (space delimited) are added,

    string - 会把列在字符串中的 CSS 类(空格分隔)添加进来,

  • Array - the CSS classes declared as Array elements are added,

    Array - 会把数组中的各个元素作为 CSS 类添加进来,

  • Object - keys are CSS classes that get added when the expression given in the value evaluates to a truthy value, otherwise they are removed.

    Object - 每个 key 都是要处理的 CSS 类,当表达式求值为真的时候则添加,为假则移除。

属性

属性名类型说明
klass
ngClass

方法

ngDoCheck(): void

参数

没有参数。

返回值

void

使用说明

<some-element [ngClass]="'first second'">...</some-element> <some-element [ngClass]="['first', 'second']">...</some-element> <some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element> <some-element [ngClass]="stringExp|arrayExp|objExp">...</some-element> <some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>