Javascript 在 angularjs 中使用 $dirty 以检查表单何时被编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30096083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using $dirty in angularjs in order to check whenever a form is being edited
提问by DarkAngeL
I was trying to check whenever my form is being edited by writing some fields of it. I read $dirty should work for that task but I can't figure out what I'm missing here:
我试图通过编写表单的某些字段来检查我的表单何时被编辑。我读到 $dirty 应该适用于该任务,但我无法弄清楚我在这里遗漏了什么:
<!DOCTYPE html>
<html lang="en">
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="formCtrl">
<form name = "myForm" novalidate>
First Name:<br>
<input type="text" ng-model="user.firstName"><br>
Last Name:<br>
<input type="text" ng-model="user.lastName">
<br><br>
<button ng-click="reset()">RESET</button>
</form>
<p> is Form dirty? {{isDirty}}<p>
<p>form = {{user }}</p>
<p>master = {{master}}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
$scope.master = {firstName:"John", lastName:"Doe"};
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
$scope.isDirty = $scope.myForm.$dirty;
});
</script>
</body>
</html>
I'm trying to make the flag isDirty to true whenever the user modifies the form. Thanks
每当用户修改表单时,我都试图将标志 isDirty 设为 true。谢谢
回答by Pankaj Parkar
You are missing nameattributes in your form fields which are not enabling form validation for those field. You need to add unique name for each field so that it will get add those field in myFormobject
您name的表单字段中缺少未为这些字段启用表单验证的属性。您需要为每个字段添加唯一名称,以便将这些字段添加到myForm对象中
Markup
标记
<form name="myForm" novalidate>
First Name:<br>
<input type="text" name="firstName" ng-model="user.firstName"><br>
Last Name:<br>
<input type="text" name="lastName" ng-model="user.lastName">
<br><br>
<button ng-click="reset()">RESET</button>
</form>
Also you are accessing myFormobject which is nothing but form object, I won't be available until DOM get rendered, $scope.myFormwill be simply undefined at the time of controller initilization, If you really want to access $scope.myFormfrom controller then you need to put that code in $timeoutthat will run $timeoutfunction code in next digest cycle.
此外,您正在访问的myForm对象只是表单对象,在 DOM 呈现之前我将不可用,$scope.myForm在控制器初始化时将只是未定义,如果您真的想$scope.myForm从控制器访问,那么您需要将该代码放入$timeout这将$timeout在下一个摘要循环中运行功能代码。
$timeout(function(){
$scope.isDirty = $scope.myForm.$dirty;
});
Update
更新
There is no need to maintain a separateisDirtyflag (this would require to changethe separate isDirtyflag to reflect any changes in myForm.$dirtyflag.) Instead I suggest you use $scope.myForm.$dirtydirectlyas a flag. So use the expression myForm.$dirty, and this flag will change as form gets dirty.
无需维护单独的isDirty标志(这将需要更改单独的isDirty标志以反映标志中的任何更改myForm.$dirty。)相反,我建议您$scope.myForm.$dirty直接将其用作标志。所以使用表达式myForm.$dirty,这个标志会随着表单变脏而改变。

