Dio*_*oss 1 forms validation angularjs
根据AngularJS(1.3.15)的来源,FormController的方法$setPristine()
将表单$submitted
状态重置为false:
form.$setPristine = function() {
$animate.setClass(element, PRISTINE_CLASS, DIRTY_CLASS + ' ' + SUBMITTED_CLASS);
form.$dirty = false;
form.$pristine = true;
form.$submitted = false;
forEach(controls, function(control) {
control.$setPristine();
});
};
Run Code Online (Sandbox Code Playgroud)
您看到此行为的原因是重置按钮没有type ="button"或type ="reset"属性,因此它默认情况下表现为提交按钮.因此,将表单设置为pristine的ng-click实际上将$ submit设置为false,但之后立即再次提交表单.
app.js
var app = angular.module('plunker', []);
app.controller('MainCtrl', function() {
this.data = {
name: ''
};
this.reset = function(form) {
this.data.name = '';
form.$setPristine();
};
});
Run Code Online (Sandbox Code Playgroud)
HTML页面:
<html ng-app="plunker">
<head>
<title>form.$submitted</title>
<script src="http://code.angularjs.org/1.3.2/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="MainCtrl as ctrl">
<form name="form" novalidate>
<input name="name" ng-model="ctrl.data.name" placeholder="Name" required />
<input type="submit" />
<button type="button" class="button" ng-click="ctrl.reset(form)">Reset</button>
</form>
<pre>
Pristine: {{form.$pristine}}
Submitted: {{form.$submitted}}
</pre>
</div>
Run Code Online (Sandbox Code Playgroud)
http://plnkr.co/edit/kRxEVu?p=preview
希望这是你想要的
资料来源:https://github.com/angular/angular.js/issues/10006#issuecomment-62640975