我有一个自定义异步验证指令,用于检查输入是否唯一。
在x = 1
s = str(x)
b = bytes(s, 'ascii')
形式中,它非常出色。 create
表单位于单击按钮时弹出的模式中。
我遇到的问题是,出于某种原因,当显示模态时异步验证器被触发三次,但我没有对值进行任何更改。我只在edit
上更新model
以防止过多的API调用,但我无法弄清楚为什么要进行前三次调用。
编辑:如果需要,我会发布代码,但asyncValidator只是一个API调用,它会返回一个promise。我传递了验证所需的第二个值,但这都是......
EDIT2:以下是代码:)
指令:
blur
用法:
var uniqueWarehouse = [
"WarehouseService", function(WarehouseService) {
return {
restrict: "A",
require: "ngModel",
scope: {
type: "=uniqueWarehouse",
id: "=uniqueWarehouseId"
},
link: function(scope, element, attrs, ctrl) {
ctrl.$asyncValidators.uniqueWarehouse = function(value) {
if (scope.id != undefined) {
return WarehouseService.Check(scope.type, value, scope.id);
} else {
return WarehouseService.Check(scope.type, value);
}
};
}
};
}
];
AppModule.directive("uniqueWarehouse ", uniqueWarehouse );
另外,我正在使用Bootstrap UI作为模态。
EDIT3:是否有可能发生这种情况,如果是,我该如何避免呢?
<div class="form-group" ng-class="{'has-error' : ( warehouseForm.$submitted || warehouseForm.title.$touched) && warehouseForm.title.$invalid }">
<label class="col-md-4 control-label" for="title">Title</label>
<div class="col-md-8">
<input class="form-control" id="title" name="title" ng-maxlength="5" ng-model="warehouse.Title" ng-model-options="{ updateOn: 'blur' }" ng-required="true" type="text" unique-warehouse="warehouse.type" unique-warehouse-id="warehouse.id" />
<div class="has-feedback help-block" ng-show="warehouseForm.title.$pending.uniqueWarehouse">Checking...</div>
<div class="help-block" ng-messages="warehouseForm.title.$error" ng-show="(warehouseForm.$submitted || warehouseForm.title.$touched) && warehouseForm.title.$invalid" role="alert">
<div ng-message="required">It is required</div>
<div ng-message="maxlength">Max length exceeded</div>
<div ng-message="uniqueWarehouse">It already exists</div>
</div>
</div>
</div>
事件被触发