【问题标题】:Validate the input field on blur with Angular使用 Angular 在模糊上验证输入字段
【发布时间】:2014-09-30 11:10:00
【问题描述】:

我做了一个简单的测试输入字段,但我试图将验证转换为模糊,但我不知道如何实现这一点,因为我对 angularjs 不太熟悉。

请任何人帮助我验证此示例中的一个模糊..

我的Js:

angular.module('myApp', [])
.controller('FormController', function($scope) {
  $scope.fields = [
    {placeholder: 'Username', isRequired: true},
    {placeholder: 'Password', isRequired: true},
    {placeholder: 'Email (optional)', isRequired: false}
  ];

  $scope.submitForm = function() {
    alert("it works!");
  };
});

html:

<div ng-app="myApp">
<form name="signup_form" ng-controller="FormController" ng-submit="submitForm()" novalidate>
  <div ng-repeat="field in fields" ng-form="signup_form_input">
    <input type="text"
           name="dynamic_input"
           ng-required="field.isRequired"
           ng-model="field.name"
           placeholder="{{field.placeholder}}" />
    <div ng-show="signup_form_input.dynamic_input.$dirty && signup_form_input.dynamic_input.$invalid">
      <span class="error" ng-show="signup_form_input.dynamic_input.$error.required">The field is required.</span>
    </div>
  </div>
  <button type="submit" ng-disabled="signup_form.$invalid">Submit All</button>
</form>
</div>

Live Demo

【问题讨论】:

    标签: javascript jquery angularjs


    【解决方案1】:

    如果您有自定义验证,一个简单的方法是使用 ngBlur。将以下内容添加到您的字段并在您的控制器中编写验证函数:

    <input type="text"
           ng-model="fieldVal" 
           ng-blur="validate(fieldVal)"/>
    

    您还可以将 ng-model-options 与 ng-change 结合使用

    <input type="text" 
           ng-modle="fildsVal" 
           ng-model-option="{ updateOn: 'blur' }" 
           ng-change="validate(fieldVal)" />
    

    我还发现 debounce 选项非常适合触发验证。您可以计时,以便在用户完成输入后触发验证:

    <input type="text"
           ng-model-options="{ debounce: 800 }" 
           ng-pattern="/^[0-9]{1,9}$/" />
    

    【讨论】:

      【解决方案2】:

      您可以简单地创建一个与ngModel 同名的指令。在将ngModel$dirty 状态更改为true 的元素上添加blur 事件。通过在 ngModel.$viewChangeListeners 数组中添加回调,确保在对元素进行更改时将 ngModel$dirty 状态更改为 false。

      FORKED SAMPLE

      指令看起来像这样:

      .directive('ngModel', function() {
          return {
              require: 'ngModel',
              link: function(scope, elem, attr, ngModel) {
                  elem.on('blur', function() {
                      ngModel.$dirty = true;
                      scope.$apply();
                  });
      
                  ngModel.$viewChangeListeners.push(function() {
                      ngModel.$dirty = false;
                  });
      
                  scope.$on('$destroy', function() {
                      elem.off('blur');
                  });
              }
          }
      });
      

      注意:如果自定义指令名称 ngModel 与 Angular 的默认 ngModel 相同,请不要担心,它只会运行它们(不会覆盖它)。 scope.$on('$destroy') 监听器在作用域被销毁时移除 blur 事件处理程序,例如当路由改变并且控制器被销毁或者当一个字段被移除时再次触发重复的DOM元素的重建(销毁由ng-repeat创建的子范围)。

      【讨论】:

        【解决方案3】:
                    angular.module('myApp', [])
                .controller('FormController', function($scope) {
                     $(document).ready(function(){
                        $("#form input").on('blur', function(){
                            alert("Cannot be null");
                        });
                    });
                  $scope.fields = [
                    {placeholder: 'Username', isRequired: true},
                    {placeholder: 'Password', isRequired: true},
                    {placeholder: 'Email (optional)', isRequired: false}
                  ];
        
                  $scope.submitForm = function() {
                    alert("it works!");
                  };
        
                });
        

        只需提供 id 即可形成并添加 javascript 代码。就这样。对于现场演示“http://jsfiddle.net/SaurabhGhewari/2ko9bamk/

        【讨论】:

        • Controller 中的 DOM 操作确实不受欢迎,因为它很难测试并且破坏了业务逻辑和表示之间的关注点分离
        • 感谢您的信息。请为此提供其他方式,以便我也可以在我的项目中进行更改。
        • 其实我刚刚发布了一个答案。
        【解决方案4】:

        如果您更新到 Angular 1.3,您可以使用 ng-model-options 更新模糊模型。

        <input type="text"
               name="dynamic_input"
               ng-required="field.isRequired"
               ng-model="field.name"
               ng-model-options="{ updateOn: 'blur' }"
               placeholder="{{field.placeholder}}" />
        

        fiddle

        但是,如果您无法更新,那么有很多方法可以做到这一点here

        【讨论】:

          猜你喜欢
          • 2015-01-29
          • 2018-03-13
          • 1970-01-01
          • 2016-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多