AngularJS form gets pristine but still submitted -
according source of angularjs (1.3.15), formcontroller's method $setpristine()
resets forms $submitted
status 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(); }); };
the problem after submitting , calling method inside controller, form reverts $submitted = false
. expected or bug?
the reason seeing behavior reset button not have type="button" or type="reset" attribute , therefore behaves submit button default. ng-click sets form pristine set $submitted false correctly, afterwards, form submitted again.
app.js
var app = angular.module('plunker', []); app.controller('mainctrl', function() { this.data = { name: '' }; this.reset = function(form) { this.data.name = ''; form.$setpristine(); }; });
html page:
<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 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>
http://plnkr.co/edit/krxevu?p=preview
hope 1 have wanted