如果元素有效且没有其他错误,我怎样才能在模糊时触发函数调用?
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");
}
});
答案 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