在表单提交之后,如果仅仅是清空表单数据内容,相应 input
的 class
并不会改变(依然是 ng-dirty
状态),我们需要移除这些红色的 required
提示。
<div ng-app="myApp" ng-controller="myCtrl as ctrl">
<form name="ctrl.myForm">
<div><label for="email">Email</label>
<input name="myInput" type="email" ng-model="ctrl.email" id="email" required></div>
<div><label for="password">Password</label>
<input name="myPassword" type="password" minlength="8" ng-model="ctrl.password" id="password" required></div>
<div>
<button ng-click="ctrl.reset()" type="button">Reset</button>
</div>
</form>
`</pre>
`JavaScript` 代码:
<pre>`
angular.module('myApp', [])
.controller('myCtrl', myCtrl);
function myCtrl(){
var vm = this;
vm.reset = function(){
vm.myForm.$setPristine();
vm.myForm.$setUntouched();
vm.email = vm.password = '';
}
}
表单input区域必须放在 form
中