Skip to content

Commit a57f470

Browse files
author
Daniel Tabuenca
committed
fix(input): ensure ngModelWatch() triggers second digest pass when appropriate
Due to an earlier change, ngModelWatch() no longer returns a value to the caller. This means the digest loop has no way to tell if the watch actually modified anything and so can not schedule another pass. This means any watches that watch form or model controller changes (e.g. watches on form.$valid) that are scheduled prior to an ngModelWatch() will not be able to see any changes made therin. This commit fixes this behavior by returning the latest evaluated ng-model value. Closes angular#5258
1 parent d802ed1 commit a57f470

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/ng/directive/input.js

+2
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
11541154
ctrl.$render();
11551155
}
11561156
}
1157+
1158+
return value;
11571159
});
11581160
}];
11591161

test/ng/directive/inputSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,29 @@ describe('ngModel', function() {
383383
dealoc(element);
384384
});
385385
});
386+
387+
it('should keep previously defined watches consistent when changes in validity are made',
388+
inject(function($compile, $rootScope) {
389+
390+
var isFormValid;
391+
$rootScope.$watch('myForm.$valid', function(value) { isFormValid = value; });
392+
393+
var element = $compile('<form name="myForm">' +
394+
'<input name="myControl" ng-model="value" required >' +
395+
'</form>')($rootScope);
396+
397+
$rootScope.$apply();
398+
expect(isFormValid).toBe(false);
399+
expect($rootScope.myForm.$valid).toBe(false);
400+
401+
$rootScope.value='value';
402+
$rootScope.$apply();
403+
expect(isFormValid).toBe(true);
404+
expect($rootScope.myForm.$valid).toBe(true);
405+
406+
dealoc(element);
407+
}));
408+
386409
});
387410

388411

0 commit comments

Comments
 (0)