|
316 | 316 | * they are waiting for their template to load asynchronously and their own compilation and linking has been
|
317 | 317 | * suspended until that occurs.
|
318 | 318 | *
|
319 |
| - * ** $doCheck example ** |
| 319 | + * ** $doCheck examples ** |
320 | 320 | *
|
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. |
322 | 323 | *
|
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"> |
324 | 367 | * <file name="index.html">
|
325 | 368 | * <div ng-init="items = []">
|
326 | 369 | * <button ng-click="items.push(items.length)">Add Item</button>
|
|
339 | 382 | * this.log = [];
|
340 | 383 | *
|
341 | 384 | * 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 | + * } |
347 | 393 | * };
|
348 | 394 | * }
|
349 | 395 | * });
|
|
0 commit comments