@@ -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,67 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
1248
1249
* - {@link ng.directive:select select}
1249
1250
* - {@link ng.directive:textarea textarea}
1250
1251
*
1252
+ * # CSS classes
1253
+ * The following CSS classes are added and removed on the associated input/select/textarea element
1254
+ * depending on the validity of the model.
1255
+ *
1256
+ * - `ng-valid` is set if the model is valid.
1257
+ * - `ng-invalid` is set if the model is invalid.
1258
+ * - `ng-pristine` is set if the model is pristine.
1259
+ * - `ng-dirty` is set if the model is dirty.
1260
+ *
1261
+ * Keep in mind that ngAnimate can detect each of these classes when added and removed.
1262
+ *
1263
+ * ## Animation Hooks
1264
+ *
1265
+ * Animations within models are triggered when any of the associated CSS classes are added and removed
1266
+ * on the input element which is attached to the model. These classes are: `.pristine`, `.dirty`,
1267
+ * `.invalid` and `.valid` as well as any other validations that are performed on the model itself.
1268
+ * The animations that are triggered within ngModel are similar to how they work in ngClass and
1269
+ * animations can be hooked into using CSS transitions, keyframes as well as JS animations.
1270
+ *
1271
+ * The following example shows a simple way to utilize CSS transitions to style an input element
1272
+ * that has been rendered as invalid after it has been validated:
1273
+ *
1274
+ * <pre>
1275
+ * //be sure to include ngAnimate as a module to hook into more
1276
+ * //advanced animations
1277
+ * .my-input {
1278
+ * transition:0.5s linear all;
1279
+ * background: white;
1280
+ * }
1281
+ * .my-input.ng-invalid {
1282
+ * background: red;
1283
+ * color:white;
1284
+ * }
1285
+ * </pre>
1286
+ *
1287
+ * @example
1288
+ * <example deps="angular-animate.js" animations="true" fixBase="true">
1289
+ <file name="index.html">
1290
+ <script>
1291
+ function Ctrl($scope) {
1292
+ $scope.val = '1';
1293
+ }
1294
+ </script>
1295
+ <style>
1296
+ .my-input {
1297
+ -webkit-transition:all linear 0.5s;
1298
+ transition:all linear 0.5s;
1299
+ background: transparent;
1300
+ }
1301
+ .my-input.ng-invalid {
1302
+ color:white;
1303
+ background: red;
1304
+ }
1305
+ </style>
1306
+ Update input to see transitions when valid/invalid.
1307
+ Integer is a valid value.
1308
+ <form name="testForm" ng-controller="Ctrl">
1309
+ <input ng-model="val" ng-pattern="/^\d+$/" name="anim" class="my-input" />
1310
+ </form>
1311
+ </file>
1312
+ * </example>
1251
1313
*/
1252
1314
var ngModelDirective = function ( ) {
1253
1315
return {
0 commit comments