Skip to content

Commit

Permalink
fix(input): ensure ngModelWatch() triggers second digest pass when ap…
Browse files Browse the repository at this point in the history
…propriate

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
  • Loading branch information
Daniel Tabuenca committed Dec 5, 2013
1 parent d802ed1 commit a57f470
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
ctrl.$render();
}
}

return value;
});
}];

Expand Down
23 changes: 23 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,29 @@ describe('ngModel', function() {
dealoc(element);
});
});

it('should keep previously defined watches consistent when changes in validity are made',
inject(function($compile, $rootScope) {

var isFormValid;
$rootScope.$watch('myForm.$valid', function(value) { isFormValid = value; });

var element = $compile('<form name="myForm">' +
'<input name="myControl" ng-model="value" required >' +
'</form>')($rootScope);

$rootScope.$apply();
expect(isFormValid).toBe(false);
expect($rootScope.myForm.$valid).toBe(false);

$rootScope.value='value';
$rootScope.$apply();
expect(isFormValid).toBe(true);
expect($rootScope.myForm.$valid).toBe(true);

dealoc(element);
}));

});


Expand Down

0 comments on commit a57f470

Please sign in to comment.