在有效模糊上调用函数

时间:2014-10-01 20:50:42

标签: angularjs angularjs-directive angularjs-scope

PLUNKER

如果元素有效且没有其他错误,我怎样才能在模糊时触发函数调用?

HTML

<input type="text" id="order" ng-model="order" name="order" class="form-control" 
    ng-pattern="/^[0-9]*$/" ng-minlength="9" maxlength="9"
    required="" ng-blur="something()"  />

JS

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
    $scope.something=function(){
      if($scope.routingNumber.$valid)
        alert("yippie");
    }
});

1 个答案:

答案 0 :(得分:6)

在这里:http://plnkr.co/edit/g4ecmWToQPbOKAk3XvNt?p=preview(完整表格) http://plnkr.co/edit/Zep8D4AXvkzwpqcFFXjR?p=preview(仅限订单字段)

您只需将form传递给something方法。

<input type="text" id="order" ng-model="order" name="order" class="form-control" 
 ng-pattern="/^[0-9]*$/" ng-minlength="9" maxlength="9"
 required="" ng-blur="something(myform)"  />

在你的JS中:

app.controller('MainCtrl', function($scope) {
  $scope.something = function(form) {
    if (form.order.$valid) {
      alert("yippie");
    }
  }
});

您可以在文档中找到有关表单验证和错误的更多信息:https://docs.angularjs.org/guide/forms