Reset form $submitted state
Call to $setPristine() in a form don't reset the from submitted state, but the expected behavior must reset the submitted state.
Code for reproduce the problem:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example33-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.18/angular.min.js"></script>
<style>.ng-submitted{background: red}</style>
</head>
<body ng-app="submitExample">
<script>
angular.module('submitExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.list = [];
$scope.text = 'hello';
$scope.submit = function() {
if ($scope.text) {
$scope.list.push(this.text);
$scope.text = '';
}
};
$scope.rs = function() {
$scope.myf.$setPristine();
}
}]);
</script>
<form ng-submit="submit()" name="myform" ng-controller="ExampleController">
Enter text and hit enter:
<input type="text" ng-model="text" name="text" />
<input type="submit" id="submit" value="Submit" />
<button ng-click="rs()">reset</button>
<pre>myform.$pristine: {{myform.$pristine}}</pre>
<pre>list={{list}}</pre>
</form>
</body>
</html>
$setPristine does work correctly. There's two issues with your code:
$scope.myfis not defined, replace it with$scope.myform- A
<button>element submits the form by default. So your "reset" button is actually also submitting the form.
To get around this, you can use<input type="button" value="Reset" />or<button type="button">Reset</button>. For more info, see button[type] on mdn
Here's your code with the above changes applied: http://plnkr.co/edit/wVZmcu0zuZTmjKa5YeWR?p=preview
In the future, try asking for help anywhere specified here before opening an issue on the issue tracker. If you had done it on IRC, you might have gotten a response instantly :-)
Closing this as work as expected.
I'm not sure this is works as expected. The class ng-submitted is never removed from the form when form.$setPristine() is called. This leaves it in a different state than it started in.
After further investigation, I found that if form.$setPristine() is called from my controller right before state.go() (ui-router) is called that there is a race condition and ng-submitted class is not removed. If i use $timeout(function(){ form.$setPristine() }); then all works as expected.
@kellyprankin thank you, your suggestion with $timeout helped me too!
@kellyprankin I was running into the exact same scenario and issue. Looks like using $timeout is the only solution I've found so far. Not pretty, but it works!
Thank for $timeout way )
I don't understand why the sourcecode for form.$setPristine includes a line form.$submitted = false; if it doesn't remove the submitted state
For me as well $timeout worked. I'm using AngularJS v1.4.9. However when I do some ajax and in callback of that ajax, I've to use $timeout, otherwise with only defined clearForm function, it works correctly.
app.controller('AddRoomTypeController', ['$scope', '$http', '$timeout', function($scope, $http, $timeout){
$scope.roomType = {};
$scope.addRoomType = function() {
$.post('api/rooms', $scope.roomType).success(function(){
alert('Saved');
$timeout(function(){
$scope.roomType = {};
$scope.frmAddRoomType.$setPristine();
$scope.frmAddRoomType.$setUntouched();
});
});
};
$scope.clearForm = function() {
$scope.roomType = {};
$scope.frmAddRoomType.$setPristine();
$scope.frmAddRoomType.$setUntouched();
};
}]);
Let me know if I am doing something wrong.