如果将输入
form与
name属性一起放入,然后为输入赋予
name属性,则还可以访问输入的
$pristine属性。
<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);});您可以
$scope.myForm.$pristine用来查看是否有 任何 字段更改,以及
$pristine表单上每个输入属性的属性来查看
该输入 是否已更改。您甚至可以遍历该
myForm对象(非输入字段对象的键前缀为
$):
angular.forEach($scope.myForm, function(value, key) { if(key[0] == '$') return; console.log(key, value.$pristine)});// first, true// last, false


