@@ -1003,8 +1003,8 @@ var VALID_CLASS = 'ng-valid',
1003
1003
*
1004
1004
*
1005
1005
*/
1006
- var NgModelController = [ '$scope' , '$exceptionHandler' , '$attrs' , '$element' , '$parse' ,
1007
- function ( $scope , $exceptionHandler , $attr , $element , $parse ) {
1006
+ var NgModelController = [ '$scope' , '$exceptionHandler' , '$attrs' , '$element' , '$parse' , '$animate' ,
1007
+ function ( $scope , $exceptionHandler , $attr , $element , $parse , $animate ) {
1008
1008
this . $viewValue = Number . NaN ;
1009
1009
this . $modelValue = Number . NaN ;
1010
1010
this . $parsers = [ ] ;
@@ -1067,9 +1067,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
1067
1067
// convenience method for easy toggling of classes
1068
1068
function toggleValidCss ( isValid , validationErrorKey ) {
1069
1069
validationErrorKey = validationErrorKey ? '-' + snake_case ( validationErrorKey , '-' ) : '' ;
1070
- $element .
1071
- removeClass ( ( isValid ? INVALID_CLASS : VALID_CLASS ) + validationErrorKey ) .
1072
- addClass ( ( isValid ? VALID_CLASS : INVALID_CLASS ) + validationErrorKey ) ;
1070
+ $animate . removeClass ( $element , ( isValid ? INVALID_CLASS : VALID_CLASS ) + validationErrorKey ) ;
1071
+ $animate . addClass ( $element , ( isValid ? VALID_CLASS : INVALID_CLASS ) + validationErrorKey ) ;
1073
1072
}
1074
1073
1075
1074
/**
@@ -1128,7 +1127,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
1128
1127
this . $setPristine = function ( ) {
1129
1128
this . $dirty = false ;
1130
1129
this . $pristine = true ;
1131
- $element . removeClass ( DIRTY_CLASS ) . addClass ( PRISTINE_CLASS ) ;
1130
+ $animate . removeClass ( $element , DIRTY_CLASS ) ;
1131
+ $animate . addClass ( $element , PRISTINE_CLASS ) ;
1132
1132
} ;
1133
1133
1134
1134
/**
@@ -1159,7 +1159,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
1159
1159
if ( this . $pristine ) {
1160
1160
this . $dirty = true ;
1161
1161
this . $pristine = false ;
1162
- $element . removeClass ( PRISTINE_CLASS ) . addClass ( DIRTY_CLASS ) ;
1162
+ $animate . removeClass ( $element , PRISTINE_CLASS ) ;
1163
+ $animate . addClass ( $element , DIRTY_CLASS ) ;
1163
1164
parentForm . $setDirty ( ) ;
1164
1165
}
1165
1166
@@ -1225,7 +1226,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
1225
1226
* require.
1226
1227
* - Providing validation behavior (i.e. required, number, email, url).
1227
1228
* - Keeping the state of the control (valid/invalid, dirty/pristine, validation errors).
1228
- * - Setting related css classes on the element (`ng-valid`, `ng-invalid`, `ng-dirty`, `ng-pristine`).
1229
+ * - Setting related css classes on the element (`ng-valid`, `ng-invalid`, `ng-dirty`, `ng-pristine`) including animations .
1229
1230
* - Registering the control with its parent {@link ng.directive:form form}.
1230
1231
*
1231
1232
* Note: `ngModel` will try to bind to the property given by evaluating the expression on the
@@ -1248,6 +1249,60 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
1248
1249
* - {@link ng.directive:select select}
1249
1250
* - {@link ng.directive:textarea textarea}
1250
1251
*
1252
+ * ## A note about animations with `NgModel`
1253
+ *
1254
+ * Animations work with the pristine, dirty, invalid and valid events that are triggered when
1255
+ * the values of input change. This system works like the animation system present with ngClass.
1256
+ *
1257
+ * <pre>
1258
+ * //
1259
+ * //a working example can be found at the bottom of this page
1260
+ * //
1261
+ * .my-element.ng-dirty-add {
1262
+ * transition:0.5s linear all;
1263
+ * background: red;
1264
+ * }
1265
+ * .my-element.ng-dirty {
1266
+ * background: white;
1267
+ * }
1268
+ *
1269
+ * .my-element.ng-dirty-add { ... }
1270
+ * .my-element.ng-dirty-add.ng-dirty-add-active { ... }
1271
+ * .my-element.ng-dirty-remove { ... }
1272
+ * .my-element.ng-dirty-remove.ng-dirty-remove-active { ... }
1273
+ * </pre>
1274
+ *
1275
+ * @animations
1276
+ * removeClass .ng-dirty and addClass .ng-pristine: happens just after input became pristine
1277
+ * removeClass .ng-pristine and addClass .ng-dirty: happens just after input became dirty
1278
+ * removeClass .ng-invalid and addClass .ng-valid: happens just after input became valid
1279
+ * removeClass .ng-valid and addClass .ng-invalid: happens just after input became invalid
1280
+ *
1281
+ * @example
1282
+ * <doc:example>
1283
+ <doc:source>
1284
+ <script>
1285
+ function Ctrl($scope) {
1286
+ $scope.val = '1';
1287
+ }
1288
+ </script>
1289
+ <style>
1290
+ input.ng-invalid-pattern-add {
1291
+ -webkit-transition:all linear 0.5s;
1292
+ transition:all linear 0.5s;
1293
+ background: red;
1294
+ }
1295
+ input.ng-invalid {
1296
+ background: white;
1297
+ }
1298
+ </style>
1299
+ Update input to see transitions when valid/invalid.
1300
+ Integer is a valid value.
1301
+ <form name="testForm" ng-controller="Ctrl">
1302
+ <input ng-model="val" ng-pattern="/^\d+$/" name="anim"/>
1303
+ </form>
1304
+ </doc:source>
1305
+ * </doc:example>
1251
1306
*/
1252
1307
var ngModelDirective = function ( ) {
1253
1308
return {
0 commit comments