Skip to content

Commit df09389

Browse files
docs(NgClass): update docs, add example
Closes angular#4520
1 parent 90191ce commit df09389

1 file changed

Lines changed: 53 additions & 13 deletions

File tree

modules/angular2/src/core/directives/ng_class.ts

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,63 @@ import {Renderer} from 'angular2/src/core/render';
1212
import {StringMapWrapper, isListLikeIterable} from 'angular2/src/core/facade/collection';
1313

1414
/**
15-
* Adds and removes CSS classes based on an {expression} value.
15+
* The `NgClass` directive conditionally adds and removes CSS classes on an HTML element based on
16+
* an expression's evaluation result.
1617
*
17-
* The result of expression is used to add and remove CSS classes using the following logic,
18-
* based on expression's value type:
19-
* - {string} - all the CSS classes (space - separated) are added
20-
* - {Array} - all the CSS classes (Array elements) are added
21-
* - {Object} - each key corresponds to a CSS class name while values
22-
* are interpreted as {boolean} expression. If a given expression
23-
* evaluates to {true} a corresponding CSS class is added - otherwise
24-
* it is removed.
18+
* The result of an expression evaluation is interpreted differently depending on type of
19+
* the expression evaluation result:
20+
* - `string` - all the CSS classes listed in a string (space delimited) are added
21+
* - `Array` - all the CSS classes (Array elements) are added
22+
* - `Object` - each key corresponds to a CSS class name while values are interpreted as expressions
23+
* evaluating to `Boolean`. If a given expression evaluates to `true` a corresponding CSS class
24+
* is added - otherwise it is removed.
2525
*
26-
* # Example:
26+
* While the `NgClass` directive can interpret expressions evaluating to `string`, `Array`
27+
* or `Object`, the `Object`-based version is the most often used and has an advantage of keeping
28+
* all the CSS class names in a template.
29+
*
30+
* ### Example ([live demo](http://plnkr.co/edit/a4YdtmWywhJ33uqfpPPn?p=preview)):
2731
*
2832
* ```
29-
* <div class="message" [ng-class]="{error: errorCount > 0}">
30-
* Please check errors.
31-
* </div>
33+
* import {Component, View, NgClass} from 'angular2/angular2';
34+
*
35+
* @Component({
36+
* selector: 'toggle-button',
37+
* inputs: ['isDisabled']
38+
* })
39+
* @View({
40+
* template: `
41+
* <div class="button" [ng-class]="{active: isOn, disabled: isDisabled}"
42+
* (click)="toggle(!isOn)">
43+
* Click me!
44+
* </div>`,
45+
* styles: [`
46+
* .button {
47+
* width: 120px;
48+
* border: medium solid black;
49+
* }
50+
*
51+
* .active {
52+
* background-color: red;
53+
* }
54+
*
55+
* .disabled {
56+
* color: gray;
57+
* border: medium solid gray;
58+
* }
59+
* `]
60+
* directives: [NgClass]
61+
* })
62+
* class ToggleButton {
63+
* isOn = false;
64+
* isDisabled = false;
65+
*
66+
* toggle(newState) {
67+
* if (!this.isDisabled) {
68+
* this.isOn = newState;
69+
* }
70+
* }
71+
* }
3272
* ```
3373
*/
3474
@Directive({selector: '[ng-class]', inputs: ['rawClass: ng-class', 'initialClasses: class']})

0 commit comments

Comments
 (0)