Skip to content

Commit a7b2ab7

Browse files
docs(NgStyle): update docs, add examples
Closes angular#4519
1 parent a0277f1 commit a7b2ab7

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

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

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,56 @@ import {Renderer} from 'angular2/src/core/render';
99
import {isPresent, isBlank, print} from 'angular2/src/core/facade/lang';
1010

1111
/**
12-
* Adds or removes styles based on an {expression}.
12+
* The `NgStyle` directive changes styles based on a result of expression evaluation.
1313
*
14-
* When the expression assigned to `ng-style` evaluates to an object, the corresponding element
15-
* styles are updated. Style names to update are taken from the object keys and values - from the
16-
* corresponding object values.
14+
* An expression assigned to the `ng-style` property must evaluate to an object and the
15+
* corresponding element styles are updated based on changes to this object. Style names to update
16+
* are taken from the object's keys, and values - from the corresponding object's values.
1717
*
18-
* # Example:
18+
* # Syntax
19+
*
20+
* - `<div [ng-style]="{'font-style': style}"></div>`
21+
* - `<div [ng-style]="styleExp"></div>` - here the `styleExp` must evaluate to an object
22+
*
23+
* ### Example ([live demo](http://plnkr.co/edit/YamGS6GkUh9GqWNQhCyM?p=preview)):
1924
*
2025
* ```
21-
* <div [ng-style]="{'text-align': alignExp}"></div>
22-
* ```
26+
* import {Component, View, NgStyle} from 'angular2/angular2';
2327
*
24-
* In the above example the `text-align` style will be updated based on the `alignExp` value
25-
* changes.
28+
* @Component({
29+
* selector: 'ng-style-example'
30+
* })
31+
* @View({
32+
* template: `
33+
* <h1 [ng-style]="{'font-style': style, 'font-size': size, 'font-weight': weight}">
34+
* Change style of this text!
35+
* </h1>
2636
*
27-
* # Syntax
37+
* <hr>
38+
*
39+
* <label>Italic: <input type="checkbox" (change)="changeStyle($event)"></label>
40+
* <label>Bold: <input type="checkbox" (change)="changeWeight($event)"></label>
41+
* <label>Size: <input type="text" [value]="size" (change)="size = $event.target.value"></label>
42+
* `,
43+
* directives: [NgStyle]
44+
* })
45+
* export class NgStyleExample {
46+
* style = 'normal';
47+
* weight = 'normal';
48+
* size = '20px';
49+
*
50+
* changeStyle($event: any) {
51+
* this.style = $event.target.checked ? 'italic' : 'normal';
52+
* }
53+
*
54+
* changeWeight($event: any) {
55+
* this.weight = $event.target.checked ? 'bold' : 'normal';
56+
* }
57+
* }
58+
* ```
2859
*
29-
* - `<div [ng-style]="{'text-align': alignExp}"></div>`
30-
* - `<div [ng-style]="styleExp"></div>`
60+
* In this example the `font-style`, `font-size` and `font-weight` styles will be updated
61+
* based on the `style` property's value changes.
3162
*/
3263
@Directive({selector: '[ng-style]', inputs: ['rawStyle: ng-style']})
3364
export class NgStyle implements DoCheck {

0 commit comments

Comments
 (0)