【问题标题】:Angular clear subform data and reset validation角度清除子表单数据和重置验证
【发布时间】:2013-09-10 00:03:06
【问题描述】:

我正在尝试使用 Angular.js 创建一个子表单 <div ng-form="vacancyForm">

有一种数据有很多字段

  • 标题
  • 可用日期
  • 价格

所有人都有required 验证。

一旦我提交了该数据,我将用它做我需要的事情,但我想重置子表单,以便所有字段都不脏,并且表单在清除字段时有效,但所有字段都是无效,因为它们现在很脏,但为空,将它们标记为无效。

示例字段

<div class="control-group" ng-class="getErrorClasses(vacancyForm.headline)">
     <label class="control-label" for="headline">Headline</label>
     <div class="controls">
         <input type="text" class="input-xlarge" id="headline" name="headline" required ng-model="new_vacancy.headline">
         <span class="help-inline" ng-show="showError(vacancyForm.headline, 'required')">This field is required</span>
      </div>
</div>

这里是提交时调用的函数

 $scope.addVacancy = function(){

        // save the submitted data
        $scope.school.vacancies.push($scope.new_vacancy);

        // now clear it out
        $scope.new_vacancy = {};
        $scope.new_vacancy.date = new Date();

        // this clears out all the fields and makes them all invalid 
        // as they are empty. how to reset the form???

    }

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    在子窗体上设置name 属性,然后您可以$scope.formName.$setPristine(); 其中formName 是name 属性。值更改后,元素不再是原始的。

    http://docs.angularjs.org/api/ng.directive:form.FormController#$setPristine

    更新
    上述答案仅适用于 1.2,但在 1.3 angular 中引入了“触摸”输入的概念。现在,当元素模糊时,角度会将字段标记为已触摸。与$setPristine类似,您可以使用$scope.formName.$setUntouched()设置回输入。

    https://docs.angularjs.org/api/ng/type/form.FormController#$setUntouched

    touched vs pristine:touched 表示该字段已被模糊,而 pristine 表示该字段的值已被修改。 Angular 的文档指出,“在将表单设置回原始状态时,将表单控件设置回未触及状态通常很有用。”

    编辑
    这是一个小提琴演示:https://jsfiddle.net/TheSharpieOne/a30kdtmo/

    angular.module('myApp', [])
      .controller('myCtrl', myCtrl);
    
    function myCtrl() {
      var vm = this;
      vm.reset = function() {
        vm.myForm.$setPristine();
        vm.myForm.$setUntouched();
        vm.email = vm.password = '';
      }
    }
    .ng-invalid.ng-touched {
      outline: 2px solid blue;
    }
    .ng-invalid.ng-dirty {
      outline: 2px solid red;
    }
    .ng-invalid.ng-dirty.ng-untouched {
      outline: 2px solid green;
    }
    form,
    form div {
      padding: 5px 10px;
    }
    h3,
    h4 {
      margin-bottom: 0;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="myCtrl as ctrl">
      <form name="ctrl.myForm">
        <div>
          <label for="email">Email</label>
          <input name="myInput" type="email" ng-model="ctrl.email" id="email" required>
        </div>
        <div>
          <label for="password">Password</label>
          <input name="myPassword" type="password" minlength="8" ng-model="ctrl.password" id="password" required>
        </div>
        <div>
          <button ng-click="ctrl.reset()" type="button">Reset</button>
        </div>
      </form>
      <div>
        <h4>Form Level</h4>
        <div>$dirty: {{ctrl.myForm.$dirty}}</div>
        <div>$pristine: {{ctrl.myForm.$pristine}}</div>
        <h4>Input Level</h4>
        <h5>Email Input</h5>
        <div>$dirty: {{ctrl.myForm.myInput.$dirty}}</div>
        <div>$pristine: {{ctrl.myForm.myInput.$pristine}}</div>
        <div>$touched: {{ctrl.myForm.myInput.$touched}}</div>
        <h5>Password Input</h5>
        <div>$dirty: {{ctrl.myForm.myPassword.$dirty}}</div>
        <div>$pristine: {{ctrl.myForm.myPassword.$pristine}}</div>
        <div>$touched: {{ctrl.myForm.myPassword.$touched}}</div>
      </div>
      <div>
        <h3>Color outlines for input</h3>
        <div title="The form loads this way, it can still be invalid since required fields are empty to start with">untouched, pristine: no outline</div>
        <div title="Such as in the middle of typing a valid email for the first time">invalid, untouched, dirty: green outline</div>
        <div title="blurred with invalid input">invalid, touched, dirty: red outline</div>
        <div title="focued and blurred without typing">invalid, touched: blue outline</div>
      </div>
    </div>

    【讨论】:

    • 嗯,但是为什么我必须使用
      标签.. 我认为 ng-form 指令的重点是用作子表单。当然我应该能够使用 $scope.vacancyForm.$setPrestine()
    • 这绝对有效,不过谢谢。此外,我发现这仅在 Angular 1.1.1 版本中引入。
    • ng-form 和 form 标签是相同的。它们在 angular 中被称为别名。很抱歉造成混乱。
    • 别忘了使用:$scope.formName.$setUntouched()。这将清除依赖于 $touched 逻辑的验证标志。
    • @chovy $scope.formName.fieldName.$setPristine();
    猜你喜欢
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多