@@ -2,10 +2,7 @@ import {ChangeDetector} from './interfaces';
22import { ChangeDetectionStrategy } from './constants' ;
33
44/**
5- * Controls change detection.
6- *
7- * {@link ChangeDetectorRef} allows requesting checks for detectors that rely on observables. It
8- * also allows detaching and attaching change detector subtrees.
5+ * Reference to a component's change detection object.
96 */
107export class ChangeDetectorRef {
118 /**
@@ -14,24 +11,184 @@ export class ChangeDetectorRef {
1411 constructor ( private _cd : ChangeDetector ) { }
1512
1613 /**
17- * Request to check all OnPush ancestors.
14+ * Marks all {@link OnPush} ancestors as to be checked.
15+ *
16+ * <!-- TODO: Add a link to a chapter on OnPush components -->
17+ *
18+ * ### Example ([live demo](http://plnkr.co/edit/GC512b?p=preview))
19+ *
20+ * ```typescript
21+ * @Component ({selector: 'cmp', changeDetection: ChangeDetectionStrategy.OnPush})
22+ * @View ({template: `Number of ticks: {{numberOfTicks}}` })
23+ * class Cmp {
24+ * numberOfTicks = 0;
25+ *
26+ * constructor(ref: ChangeDetectorRef) {
27+ * setInterval(() => {
28+ * this.numberOfTicks ++
29+ * // the following is required, otherwise the view will not be updated
30+ * this.ref.markForCheck();
31+ * }, 1000);
32+ * }
33+ * }
34+ *
35+ * @Component ({
36+ * selector: 'app',
37+ * changeDetection: ChangeDetectionStrategy.OnPush
38+ * })
39+ * @View ({
40+ * template: `
41+ * <cmp><cmp>
42+ * `,
43+ * directives: [Cmp]
44+ * })
45+ * class App {
46+ * }
47+ *
48+ * bootstrap(App);
49+ * ```
1850 */
1951 markForCheck ( ) : void { this . _cd . markPathToRootAsCheckOnce ( ) ; }
2052
2153 /**
2254 * Detaches the change detector from the change detector tree.
2355 *
2456 * The detached change detector will not be checked until it is reattached.
57+ *
58+ * This can also be used in combination with {@link detectChanges} to implement local change
59+ * detection checks.
60+ *
61+ * <!-- TODO: Add a link to a chapter on detach/reattach/local digest -->
62+ * <!-- TODO: Add a live demo once ref.detectChanges is merged into master -->
63+ *
64+ * ### Example
65+ *
66+ * The following example defines a component with a large list of readonly data.
67+ * Imagine the data changes constantly, many times per second. For performance reasons,
68+ * we want to check and update the list every five seconds. We can do that by detaching
69+ * the component's change detector and doing a local check every five seconds.
70+ *
71+ * ```typescript
72+ * class DataProvider {
73+ * // in a real application the returned data will be different every time
74+ * get data() {
75+ * return [1,2,3,4,5];
76+ * }
77+ * }
78+ *
79+ * @Component ({selector: 'giant-list'})
80+ * @View ({
81+ * template: `
82+ * <li *ng-for="#d of dataProvider.data">Data {{d}}</lig>
83+ * `,
84+ * directives: [NgFor]
85+ * })
86+ * class GiantList {
87+ * constructor(private ref: ChangeDetectorRef, private dataProvider:DataProvider) {
88+ * ref.detach();
89+ * setInterval(() => {
90+ * this.ref.detectChanges();
91+ * }, 5000);
92+ * }
93+ * }
94+ *
95+ * @Component ({
96+ * selector: 'app', bindings: [DataProvider]
97+ * })
98+ * @View ({
99+ * template: `
100+ * <giant-list><giant-list>
101+ * `,
102+ * directives: [GiantList]
103+ * })
104+ * class App {
105+ * }
106+ *
107+ * bootstrap(App);
108+ * ```
25109 */
26110 detach ( ) : void { this . _cd . mode = ChangeDetectionStrategy . Detached ; }
27111
112+ /**
113+ * Checks the change detector and its children.
114+ *
115+ * This can also be used in combination with {@link detach} to implement local change detection
116+ * checks.
117+ *
118+ * <!-- TODO: Add a link to a chapter on detach/reattach/local digest -->
119+ * <!-- TODO: Add a live demo once ref.detectChanges is merged into master -->
120+ *
121+ * ### Example
122+ *
123+ * The following example defines a component with a large list of readonly data.
124+ * Imagine, the data changes constantly, many times per second. For performance reasons,
125+ * we want to check and update the list every five seconds.
126+ *
127+ * We can do that by detaching the component's change detector and doing a local change detection
128+ * check
129+ * every five seconds.
130+ *
131+ * See {@link detach} for more information.
132+ */
28133 detectChanges ( ) : void { this . _cd . detectChanges ( ) ; }
29134
30135 /**
31136 * Reattach the change detector to the change detector tree.
32137 *
33- * This also requests a check of this change detector . This reattached change detector will be
138+ * This also marks OnPush ancestors as to be checked . This reattached change detector will be
34139 * checked during the next change detection run.
140+ *
141+ * <!-- TODO: Add a link to a chapter on detach/reattach/local digest -->
142+ *
143+ * ### Example ([live demo](http://plnkr.co/edit/aUhZha?p=preview))
144+ *
145+ * The following example creates a component displaying `live` data. The component will detach
146+ * its change detector from the main change detector tree when the component's live property
147+ * is set to false.
148+ *
149+ * ```typescript
150+ * class DataProvider {
151+ * data = 1;
152+ *
153+ * constructor() {
154+ * setInterval(() => {
155+ * this.data = this.data * 2;
156+ * }, 500);
157+ * }
158+ * }
159+ *
160+ * @Component ({selector: 'live-data', properties: ['live']})
161+ * @View ({
162+ * template: `Data: {{dataProvider.data}}`
163+ * })
164+ * class LiveData {
165+ * constructor(private ref: ChangeDetectorRef, private dataProvider:DataProvider) {}
166+ *
167+ * set live(value) {
168+ * if (value)
169+ * this.ref.reattach();
170+ * else
171+ * this.ref.detach();
172+ * }
173+ * }
174+ *
175+ * @Component ({
176+ * selector: 'app',
177+ * bindings: [DataProvider]
178+ * })
179+ * @View ({
180+ * template: `
181+ * Live Update: <input type="checkbox" [(ng-model)]="live">
182+ * <live-data [live]="live"><live-data>
183+ * `,
184+ * directives: [LiveData, FORM_DIRECTIVES]
185+ * })
186+ * class App {
187+ * live = true;
188+ * }
189+ *
190+ * bootstrap(App);
191+ * ```
35192 */
36193 reattach ( ) : void {
37194 this . _cd . mode = ChangeDetectionStrategy . CheckAlways ;
0 commit comments