@@ -1956,6 +1956,9 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
1956
1956
*
1957
1957
* @description
1958
1958
* Runs each of the registered validators (first synchronous validators and then asynchronous validators).
1959
+ * If the validity changes to invalid, the model will be set to undefined, unless ngModelOptions.allowInvalid
1960
+ * is `true`. If the validity changes to valid, it will set the model to the last available valid
1961
+ * modelValue.
1959
1962
*/
1960
1963
this . $validate = function ( ) {
1961
1964
// ignore $validate before model is initialized
@@ -1964,12 +1967,16 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
1964
1967
}
1965
1968
1966
1969
var viewValue = ctrl . $$lastCommittedViewValue ;
1967
- var modelValue = ctrl . $rawModelValue ;
1970
+ var modelValue = ctrl . $$rawModelValue ;
1971
+
1968
1972
var prevValid = ctrl . $valid ;
1969
1973
var prevModelValue = ctrl . $modelValue ;
1974
+
1970
1975
var allowInvalid = ctrl . $options && ctrl . $options . allowInvalid ;
1971
1976
1972
1977
ctrl . $$runValidators ( undefined , modelValue , viewValue , function ( allValid ) {
1978
+ // If there was no change in validity, don't update the model
1979
+ // This prevents changing an invalid modelValue to undefined
1973
1980
if ( ! allowInvalid && prevValid !== allValid ) {
1974
1981
// Note: Don't check ctrl.$valid here, as we could have
1975
1982
// external validators (e.g. calculated on the server),
@@ -2130,7 +2137,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
2130
2137
}
2131
2138
var prevModelValue = ctrl . $modelValue ;
2132
2139
var allowInvalid = ctrl . $options && ctrl . $options . allowInvalid ;
2133
- ctrl . $rawModelValue = modelValue ;
2140
+ ctrl . $$ rawModelValue = modelValue ;
2134
2141
if ( allowInvalid ) {
2135
2142
ctrl . $modelValue = modelValue ;
2136
2143
writeToModelIfNeeded ( ) ;
@@ -2255,7 +2262,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
2255
2262
// if scope model value and ngModel value are out of sync
2256
2263
// TODO(perf): why not move this to the action fn?
2257
2264
if ( modelValue !== ctrl . $modelValue ) {
2258
- ctrl . $modelValue = ctrl . $rawModelValue = modelValue ;
2265
+ ctrl . $modelValue = ctrl . $$ rawModelValue = modelValue ;
2259
2266
2260
2267
var formatters = ctrl . $formatters ,
2261
2268
idx = formatters . length ;
0 commit comments