重置ngModel的有效性

时间:2015-04-16 14:15:08

标签: angularjs reset

最近,我在Angular表单有效性方面遇到了问题。我可以使用帮助ngModel。$ setValidity将数据条目添加到数组中,但我无法将其删除。我的html标签有很多有效/无效的类。我试着把表格做成原始的。但它确实有效。一般怎么办?请帮帮我! (对不起我的英语=)如果我在某个地方犯了错误。)

2 个答案:

答案 0 :(得分:4)

它没有很好的文档记录,但您实际上可以将null传递给$setValidity()函数,以便完全清除验证标记。

如果您想将其设置为有效,则只需传入true

//Reset validity for this control
this.form.firstName.$setValidity('someValidator', null);

//Or set to valid
this.form.firstName.$setValidity('someValidator', true);

这是一个运行代码片段来演示这种技术。



(function() {
  'use strict';

  function MainCtrl() {
    this.firstName = 'Josh';
  }
  MainCtrl.prototype = {
    setInvalid: function(ctrl) {
      ctrl.$setValidity('customValidator', false);
    },
    setPristine: function(ctrl) {
      ctrl.$setValidity('customValidator', null);
    },
  };

  angular.module('sample', [])
    .controller('MainCtrl', MainCtrl);


}());

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<div class="container" ng-app="sample" ng-controller="MainCtrl as ctrl">
  <div class="row">
    <div class="col-sm-12">
      <form name="ctrl.form">
        <div class="form-group" ng-class="{'has-error':ctrl.form.firstName.$invalid}">
          <label class="control-label">First Name</label>
          <input type="text" class="form-control" name="firstName" ng-model="ctrl.firstName" />
        </div>
        <button type="button" class="btn btn-danger" ng-click="ctrl.setInvalid(ctrl.form.firstName)">Set Invalid</button>
        <button type="button" class="btn btn-success" ng-click="ctrl.setPristine(ctrl.form.firstName)">Set Valid</button>
      </form>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

&#13;
&#13;
(function () {
    angular.module("App")
        .directive("password", password);

    function password() {

        var lastTrueRegex = {};

        var regexes = {
            week: /(?=^.{8,}$).*$/,
            pettyWeek: /(?=^.{8,}$)(?=.*\d).*$/,
            normal: /(?=^.{8,}$)(?=.*\d)(?=.*[a-z]).*$/,
            prettyStrong: /(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/,
            strong: /(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?![.\n]).*$/,
            veryStrong: /(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?![.\n])(?=.*[!@#$%^&*]+).*$/
        };

        function forEach(data, callback) {
            for (var key in data) {
                if (data.hasOwnProperty(key)) {
                    callback(key, data[key]);
                }
            }
        }

        return {
            require: "ngModel",
            restrict: 'EA',
            link: function (scope, element, attributes, ngModel) {

                ngModel.$parsers.unshift(function (textValue) {

                    forEach(regexes, function (key, regex) {
                        if (regex.test(textValue)){
                            lastTrueRegex.name = key;
                            lastTrueRegex.value = true;
                        }else{
                            ngModel.$setValidity(key, null);
                        }
                    });

                    if (lastTrueRegex.name){
                        ngModel.$setValidity(lastTrueRegex.name, lastTrueRegex.value);
                        return textValue;
                    }
                });

                ngModel.$formatters.unshift(function (modelValue) {
                    //ngModel.$setValidity('strongPass', isValid(modelValue));
                    return modelValue;
                });
            }
        };
    }
})();
&#13;
<form name="myForm">
    <input type="text" name="password" ng-model="textValue" password/>
</form>
&#13;
&#13;
&#13;