The ngModel
directive binds an input
,select
, textarea
(or custom form control) to a
property on the scope using NgModelController,
which is created and exposed by this directive.
ngModel
is responsible for:
input
, textarea
or select
require.ng-valid
, ng-invalid
, ng-dirty
, ng-pristine
, ng-touched
,
ng-untouched
, ng-empty
, ng-not-empty
) including animations.Note: ngModel
will try to bind to the property given by evaluating the expression on the
current scope. If the property doesn't already exist on this scope, it will be created
implicitly and added to the scope.
For best practices on using ngModel
, see:
For basic examples, how to use ngModel
, see:
By default, ngModel
watches the model by reference, not value. This is important to know when
binding inputs to models that are objects (e.g. Date
) or collections (e.g. arrays). If only properties of the
object or collection change, ngModel
will not be notified and so the input will not be re-rendered.
The model must be assigned an entirely new object or collection before a re-rendering will occur.
Some directives have options that will cause them to use a custom $watchCollection
on the model expression
ngOptions
will do so when a track by
clause is included in the comprehension expression or
if the select is given the multiple
attribute.The $watchCollection()
method only does a shallow comparison, meaning that changing properties deeper than the
first level of the object (or only changing the properties of an item in the collection if it's an array) will still
not trigger a re-rendering of the model.
The following CSS classes are added and removed on the associated input/select/textarea element depending on the validity of the model.
ng-valid
: the model is validng-invalid
: the model is invalidng-valid-[key]
: for each valid key added by $setValidity
ng-invalid-[key]
: for each invalid key added by $setValidity
ng-pristine
: the control hasn't been interacted with yetng-dirty
: the control has been interacted withng-touched
: the control has been blurredng-untouched
: the control hasn't been blurredng-pending
: any $asyncValidators
are unfulfilledng-empty
: the view does not contain a value or the value is deemed "empty", as defined
by the ngModel.NgModelController
methodng-not-empty
: the view contains a non-empty valueKeep in mind that ngAnimate can detect each of these classes when added and removed.
Animations within models are triggered when any of the associated CSS classes are added and removed
on the input element which is attached to the model. These classes include: .ng-pristine
, .ng-dirty
,
.ng-invalid
and .ng-valid
as well as any other validations that are performed on the model itself.
The animations that are triggered within ngModel are similar to how they work in ngClass and
animations can be hooked into using CSS transitions, keyframes as well as JS animations.
The following example shows a simple way to utilize CSS transitions to style an input element that has been rendered as invalid after it has been validated:
//be sure to include ngAnimate as a module to hook into more //advanced animations .my-input { transition:0.5s linear all; background: white; } .my-input.ng-invalid { background: red; color:white; }
<input>
...
</input>
<script>
angular.module('inputExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.val = '1';
}]);
</script>
<style>
.my-input {
transition:all linear 0.5s;
background: transparent;
}
.my-input.ng-invalid {
color:white;
background: red;
}
</style>
<p id="inputDescription">
Update input to see transitions when valid/invalid.
Integer is a valid value.
</p>
<form name="testForm" ng-controller="ExampleController">
<input ng-model="val" ng-pattern="/^\d+$/" name="anim" class="my-input"
aria-describedby="inputDescription" />
</form>
Sometimes it's helpful to bind ngModel
to a getter/setter function. A getter/setter is a
function that returns a representation of the model when called with zero arguments, and sets
the internal state of a model when called with an argument. It's sometimes useful to use this
for models that have an internal representation that's different from what the model exposes
to the view.
You use this behavior by adding ng-model-options="{ getterSetter: true }"
to an element that
has ng-model
attached to it. You can also add ng-model-options="{ getterSetter: true }"
to
a <form>
, which will enable this behavior for all <input>
s within it. See
ngModelOptions
for more.
The following example shows how to use ngModel
with a getter/setter:
<div ng-controller="ExampleController">
<form name="userForm">
<label>Name:
<input type="text" name="userName"
ng-model="user.name"
ng-model-options="{ getterSetter: true }" />
</label>
</form>
<pre>user.name = <span ng-bind="user.name()"></span></pre>
</div>
angular.module('getterSetterExample', [])
.controller('ExampleController', ['$scope', function($scope) {
var _name = 'Brian';
$scope.user = {
name: function(newName) {
// Note that newName can be undefined for two reasons:
// 1. Because it is called as a getter and thus called with no arguments
// 2. Because the property should actually be set to undefined. This happens e.g. if the
// input is invalid
return arguments.length ? (_name = newName) : _name;
}
};
}]);