If you put the input in a form
with a name
attribute and then give the input a name
attribute, you can also access the input's $pristine
property.
<div ng-controller="MyController">
<form name="myForm">
<input type="text" name="first" ng-model="firstName">
<input type="text" name="last" ng-model="lastName">
</form>
</div>
app.controller('MyController', function($scope) {
// Here you have access to the inputs' `$pristine` property
console.log($scope.myForm.first.$pristine);
console.log($scope.myForm.last.$pristine);
});
You can use $scope.myForm.$pristine
to see if any fields have changed, and the $pristine
property on each input's property on the form to see if that input has changed. You can even iterate over the myForm
object (non-input-field objects have keys prefixed with a $
):
angular.forEach($scope.myForm, function(value, key) {
if(key[0] == '$') return;
console.log(key, value.$pristine)
});
// first, true
// last, false