AngularJS将表单设置为初始状态

时间:2016-08-04 08:19:44

标签: angularjs forms

我有一个表单,一旦提交,我想清除所有字段并设置为初始状态。

我使用$scope.form.setPristine();但字段标签保持红色。

enter image description here

我该如何避免这种情况?

修改

我在这里发布代码。除了这样的问题,一切正常。

HTML

<form name="change_password" novalidate>
  <md-input-container style="margin:0;width:200px" flex>
    <label>Enter your current password</label>
    <input name="password" type="password" ng-model="password" ng-minlength="7" required>

    <div ng-messages="change_password.password.$error" ng-if="change_password.password.$dirty" role="alert">
      <div class="error_form" ng-message="required">Enter your current password.</div>
      <div class="error_form" ng-message="minlength">Password must be at least 7 characters long.</div>
    </div>
  </md-input-container>

  <md-input-container style="margin:0;width:200px" flex>
    <label>New Password</label>
    <input name="new_password" type="password" ng-model="new_password" ng-minlength="7" required>

    <div ng-messages="change_password.new_password.$error" ng-if="change_password.new_password.$dirty" role="alert">
      <div class="error_form" ng-message="required">Enter your new password.</div>
      <div class="error_form" ng-message="minlength">New password must be at least 7 characters long.</div>
    </div>
  </md-input-container>

  <md-input-container style="margin:0;width:200px" flex>
    <label>Confirm new password</label>
    <input name="re_new_password" type="password" ng-model="re_new_password" ng-minlength="7" required>

    <div ng-messages="change_password.re_new_password.$error" ng-if="change_password.re_new_password.$dirty" role="alert">
      <div class="error_form" ng-message="required">Confirm your new password.</div>
      <div class="error_form" ng-message="minlength">New password must be at least 7 characters long.</div>
    </div>
  </md-input-container>

  <button  class="button" style="width:200px" ng-if="!saving" ng-click="save_password()" ng-disabled="is_uploading || change_password.$invalid || new_password!=re_new_password">Save new password</button>
  <div ng-if="saving" layout="row" layout-align="center center">
    <md-progress-circular md-mode="indeterminate" size="22"></md-progress-circular>
  </div>
</form>

控制

userService.save_password($scope.password, $scope.new_password).then( function(response) {
  $scope.$apply( function() {
    if ( response.result ) {
      $scope.password = '';
      $scope.new_password = '';
      $scope.re_new_password = '';
      console.info($scope.change_password);
      $scope.change_password.$setPristine();
    }
    showMessage(response);
    $scope.saving = false;
  })
})

2 个答案:

答案 0 :(得分:1)

你误解了

setPristine();

它只清除表单上的类,没有别的。变量仍然设置。你需要这样做:

$scope.emptyModel = {};
$scope.reset = function() {
   $scope.yourFormModel = angular.copy($scope.emptyModel);
   $scope.user_form.$setPristine();
   $scope.user_form.$setUntouched();
}

答案 1 :(得分:0)

使用$ setPristine和$ setUntouched(为你的小提琴)重置:

    $scope.user_form.$setPristine();
    $scope.user_form.$setUntouched();