Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit fd57df1

Browse files
docs($compile): add additional runnable examples for the $doCheck hook
Closes #14811
1 parent 3010ed4 commit fd57df1

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

src/ng/compile.js

+54-8
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,54 @@
316316
* they are waiting for their template to load asynchronously and their own compilation and linking has been
317317
* suspended until that occurs.
318318
*
319-
* ** $doCheck example **
319+
* ** $doCheck examples **
320320
*
321-
* This example show how you might use `$doCheck` to customise the equality check of component inputs.
321+
* This example shows how you can check for mutations to a Date object even though the identity of the object
322+
* has not changed.
322323
*
323-
* <example name="doCheckExample" module="do-check-module">
324+
* <example name="doCheckDateExample" module="do-check-module">
325+
* <file name="app.js">
326+
* angular.module('do-check-module', [])
327+
* .component('app', {
328+
* template:
329+
* 'Month: <input ng-model="$ctrl.month" ng-change="$ctrl.updateDate()">' +
330+
* 'Date: {{ $ctrl.date }}' +
331+
* '<test date="$ctrl.date"></test>',
332+
* controller: function() {
333+
* this.date = new Date();
334+
* this.month = this.date.getMonth();
335+
* this.updateDate = function() {
336+
* this.date.setMonth(this.month);
337+
* };
338+
* }
339+
* })
340+
* .component('test', {
341+
* bindings: { date: '<' },
342+
* template:
343+
* '<pre>{{ $ctrl.log | json }}</pre>',
344+
* controller: function() {
345+
* var previousValue;
346+
* this.log = [];
347+
* this.$doCheck = function() {
348+
* var currentValue = this.date && this.date.valueOf();
349+
* if (previousValue !== currentValue) {
350+
* this.log.push('doCheck: date mutated: ' + this.date);
351+
* previousValue = currentValue;
352+
* }
353+
* };
354+
* }
355+
* });
356+
* </file>
357+
* <file name="index.html">
358+
* <app></app>
359+
* </file>
360+
* </example>
361+
*
362+
* This example show how you might use `$doCheck` to trigger changes in your component's inputs even if the
363+
* actual identity of the component doesn't change. (Be aware that cloning and deep equality checks on large
364+
* arrays or objects can have a negative impact on your application performance)
365+
*
366+
* <example name="doCheckArrayExample" module="do-check-module">
324367
* <file name="index.html">
325368
* <div ng-init="items = []">
326369
* <button ng-click="items.push(items.length)">Add Item</button>
@@ -339,11 +382,14 @@
339382
* this.log = [];
340383
*
341384
* this.$doCheck = function() {
342-
* if (this.items_ref !== this.items) { this.log.push('doCheck: items changed'); }
343-
* if (!angular.equals(this.items_clone, this.items)) { this.log.push('doCheck: items mutated'); }
344-
*
345-
* this.items_clone = angular.copy(this.items);
346-
* this.items_ref = this.items;
385+
* if (this.items_ref !== this.items) {
386+
* this.log.push('doCheck: items changed');
387+
* this.items_ref = this.items;
388+
* }
389+
* if (!angular.equals(this.items_clone, this.items)) {
390+
* this.log.push('doCheck: items mutated');
391+
* this.items_clone = angular.copy(this.items);
392+
* }
347393
* };
348394
* }
349395
* });

0 commit comments

Comments
 (0)