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

Commit f39e519

Browse files
zbjornsonpetebacondarwin
authored andcommitted
feat($compile): backport $doCheck
Backuport ngDoCheck from Angular 2.
1 parent d406a15 commit f39e519

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/ng/compile.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@
300300
* `changesObj` is a hash whose keys are the names of the bound properties that have changed, and the values are an
301301
* object of the form `{ currentValue, previousValue, isFirstChange() }`. Use this hook to trigger updates within a
302302
* component such as cloning the bound value to prevent accidental mutation of the outer value.
303+
* * `$doCheck()` - Called on each digest cycle. Provides an opportunity to detect and act on
304+
* changes. Any actions that you wish to take in response to the changes that you detect must be
305+
* invoked from this hook; implementing this has no effect on when `$onChanges` is called.
303306
* * `$onDestroy()` - Called on a controller when its containing scope is destroyed. Use this hook for releasing
304307
* external resources, watches and event handlers. Note that components have their `$onDestroy()` hooks called in
305308
* the same order as the `$scope.$broadcast` events are triggered, which is top down. This means that parent
@@ -2499,6 +2502,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
24992502
$exceptionHandler(e);
25002503
}
25012504
}
2505+
if (isFunction(controllerInstance.$doCheck)) {
2506+
controllerInstance.$doCheck();
2507+
}
25022508
if (isFunction(controllerInstance.$onDestroy)) {
25032509
controllerScope.$on('$destroy', function callOnDestroyHook() {
25042510
controllerInstance.$onDestroy();
@@ -3151,7 +3157,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
31513157
forEach(bindings, function initializeBinding(definition, scopeName) {
31523158
var attrName = definition.attrName,
31533159
optional = definition.optional,
3154-
mode = definition.mode, // @, =, or &
3160+
mode = definition.mode, // @, =, <, or &
31553161
lastValue,
31563162
parentGet, parentSet, compare, removeWatch;
31573163

@@ -3263,6 +3269,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
32633269
}
32643270
});
32653271

3272+
if (isFunction(destination.$doCheck)) {
3273+
var doCheckWatch = scope.$watch(triggerDoCheckHook);
3274+
removeWatchCollection.push(doCheckWatch);
3275+
}
3276+
32663277
function recordChanges(key, currentValue, previousValue) {
32673278
if (isFunction(destination.$onChanges) && currentValue !== previousValue) {
32683279
// If we have not already scheduled the top level onChangesQueue handler then do so now
@@ -3290,6 +3301,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
32903301
changes = undefined;
32913302
}
32923303

3304+
function triggerDoCheckHook() {
3305+
destination.$doCheck();
3306+
}
3307+
32933308
return {
32943309
initialChanges: initialChanges,
32953310
removeWatches: removeWatchCollection.length && function removeWatches() {

test/ng/compileSpec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,6 +3828,51 @@ describe('$compile', function() {
38283828
});
38293829
});
38303830

3831+
describe('$doCheck', function() {
3832+
it('should call `$doCheck`, if provided, for each digest cycle, after $onChanges and $onInit', function() {
3833+
var log = [];
3834+
3835+
function TestController() { }
3836+
TestController.prototype.$doCheck = function() { log.push('$doCheck'); };
3837+
TestController.prototype.$onChanges = function() { log.push('$onChanges'); };
3838+
TestController.prototype.$onInit = function() { log.push('$onInit'); };
3839+
3840+
angular.module('my', [])
3841+
.component('dcc', {
3842+
controller: TestController,
3843+
bindings: { 'prop1': '<' }
3844+
});
3845+
3846+
module('my');
3847+
inject(function($compile, $rootScope) {
3848+
element = $compile('<dcc prop1="val"></dcc>')($rootScope);
3849+
expect(log).toEqual([
3850+
'$onChanges',
3851+
'$onInit',
3852+
'$doCheck'
3853+
]);
3854+
3855+
// Clear log
3856+
log = [];
3857+
3858+
$rootScope.$apply();
3859+
expect(log).toEqual([
3860+
'$doCheck',
3861+
'$doCheck'
3862+
]);
3863+
3864+
// Clear log
3865+
log = [];
3866+
3867+
$rootScope.$apply('val = 2');
3868+
expect(log).toEqual([
3869+
'$doCheck',
3870+
'$onChanges',
3871+
'$doCheck'
3872+
]);
3873+
});
3874+
});
3875+
});
38313876

38323877
describe('$onChanges', function() {
38333878

0 commit comments

Comments
 (0)