angularjs form reset error

Use the following to reset dirty state

$scope.form.$setPristine();

Use the following to reset to clear validation

$scope.form.$setValidity();

There's API documentation on the FormController.

This allowed me to find that there's other methods to call such as:

$setUntouched() - which is a function I was using if the user has focused on the field, and then left the field, this clears this feature when you run it.

I created a simple form reset function which you can use too.

// Set the following in your controller for the form/page.
// Allows you to set default form values on fields.
$scope.defaultFormData = { username : 'Bob'}
// Save a copy of the defaultFormData
$scope.resetCopy = angular.copy($scope.defaultFormData);
// Create a method to reset the form back to it's original state.
$scope.resetForm =  function() {
    // Set the field values back to the original default values
    $scope.defaultFormData = angular.copy($scope.resetCopy);
    $scope.myForm.$setPristine();
    $scope.myForm.$setValidity();
    $scope.myForm.$setUntouched();
    // in my case I had to call $apply to refresh the page, you may also need this.
    $scope.$apply();
}

In your form, this simple setup will allow you to reset the form

<form ng-submit="doSomethingOnSubmit()" name="myForm">
    <input type="text" name="username" ng-model="username" ng-required />
    <input type="password" name="password" ng-model="password" ng-required />
    <button type="button" ng-click="resetForm()">Reset</button>
    <button type="submit">Log In</button>
</form>

var original = $scope.user;

when resetting :

$scope.user= angular.copy(original);
$scope.userForm.$setPristine();

remove

type='reset' in  <button>

here is the Angular Documentation for form controllers.


I went with...

$scope.form.$setPristine();
$scope.form.$error = {};

Feels hacky... but a lot about angular does.

Besides... this was the only thing that worked.