I am struggling with resetting an Angular Material design form that has 3 input fields with the required
option. Despite using $setPristine
to change the form to its default state after clicking the submit button, error lines continue to appear in red. Is there a solution to remove these errors post submission?
Your help is greatly appreciated.
Access the Codepen Link: http://codepen.io/sateesh2499/pen/pbkjVV
Here's the view:
<div ng-controller="AppCtrl" layout="column" ng-cloak="" class="inputdemoErrors" ng-app="MyApp">
<form name="careersForm">
<div class="careersContainer">
<md-content>
<div layout-gt-sm="row">
<md-input-container class="md-block" flex-gt-sm>
<label>Job Title</label>
<input type="text" name="jobTitle" ng-model="careers.jobTitle" required>
</md-input-container>
<md-input-container class="md-block" flex-gt-sm>
<label>Job Location</label>
<input type="text" name="jobLocation" ng-model="careers.jobLocation" required>
</md-input-container>
<md-input-container class="md-block" flex-gt-sm>
<label>Job Category</label>
<input type="text" name="jobCategory" ng-model="careers.jobCategory" required>
</md-input-container>
</div>
</md-content>
</div>
<div class="row text-center">
<div class="col-sm-12">
<md-button class="md-raised" style="width: 200px"
ng-click="postJob()">Submit</md-button>
</div>
</div>
</form>
Pristine: {{careersForm.$pristine}}
</div>
The Controller Logic:
angular.module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])
.controller('AppCtrl', function($scope) {
$scope.careers = {};
$scope.postJob = function(){
// reset form after successful submission
$scope.careers = {};
$scope.careersForm.$setPristine();
}
});