Do AsyncValidators fire all the time?

2019-03-05 04:50发布

I am using angular.js 1.6.4 version. I created a directive for server-side validation and I see that it's firing when I am loading the form, which is wrong. I only want to fire my code when I changed the value. My HTML code is

<div class="col-xs-6">
            <div class="controls">
                <label class="control-label"
                       ng-hide="editRetailTrackingForm.barcode.$dirty && editRetailTrackingForm.barcode.$error">
                    @Labels.barcode:
                </label>
                <input type="text" name="barcode"
                       ng-model="currentItem.barcode"
                       sm-code-unique-validator
                       table-name="items"
                       column-to-test="barcode"
                       error-message="itemBarcodeErrorMessage"
                       primary-key="currentItem.itemId"
                       ng-model-options="{  updateOn: 'blur', debounce: { default : 500, blur: 0 }}"
                       class="form-control"
                       ng-maxlength="100" />
                
                <div ng-class="{loading:editRetailTrackingForm.barcode.$pending}">@String.Format(Messages.validatingX, Labels.barcode)</div>

                <div ng-if="editRetailTrackingForm.barcode.$dirty">
                    <div ng-messages="editRetailTrackingForm.barcode.$error">
                        <div ng-message="maxlength">
                            <label class="field-validation-error control-label-error animate-show">
                                @String.Format(Messages.cannotExceed, Labels.barcode, "100")
                            </label>
                        </div>
                        <div ng-message="smCodeUnique">
                            <div class="info-text-block-error">
                                {{itemBarcodeErrorMessage}}
                            </div>
                        </div>
                    </div>
                </div>
                               

            </div>
        </div>

and my directive code is very similar to http://www.codelord.net/2014/11/02/angularjs-1-dot-3-taste-async-validators/

 return {

            require: "ngModel",

            scope: {
                primaryKey: "=?",
                tableName: "@",
                columnToTest: "@",
                errorMessage: "=?"
            },

            link: function ($scope, element, attrs, ngModel) {

                ngModel.$asyncValidators.smCodeUnique = function (modelValue, viewValue) {
                    if (!viewValue) {
                        return true;
                    }

                    let codeObject = {
                        id: $scope.primaryKey, tableName: $scope.tableName,
                        columnToTest: $scope.columnToTest, code: viewValue
                    };
                    return services.Http.put('api/items/checkCodeUniqueness', codeObject).then(
                        function (response) {
                            if (!response.data.isValid) {
                                $scope.errorMessage = response.data.errorMessage;
                                return services.Deferred.reject(response.data.errorMessage);
                            }
                            return true;
                        }
                    );
                };
            }
        };

When I am debugging my code I see that server side code is running multiple times when I only instantiate my form (and not even open the correct tab where this field is).

So, how should I change my directive or ng-options to only fire validations when I changed my value?

0条回答
登录 后发表回答