Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs指令中最后一个实际执行的异步调用_Javascript_Angularjs - Fatal编程技术网

Javascript Angularjs指令中最后一个实际执行的异步调用

Javascript Angularjs指令中最后一个实际执行的异步调用,javascript,angularjs,Javascript,Angularjs,注意:我使用的是AngularJS 1.2 我有一个表单,其中每个字段都会在值更改时点击服务器进行验证 下面的html代码段(假设所有目的都正确设置了html): 为什么只执行最后一个调用?打开“网络”选项卡,我只看到一个类型为“foursquare”的服务器调用。找到了答案!与限制的资源有关…显然,将解Bouncems设置为0不足以阻止它执行解Bouncing部分。我切换回$resource,一切正常(但这也意味着onValueChanged的去边界部分不起作用) <

注意:我使用的是AngularJS 1.2

我有一个表单,其中每个字段都会在值更改时点击服务器进行验证

下面的html代码段(假设所有目的都正确设置了html):




为什么只执行最后一个调用?打开“网络”选项卡,我只看到一个类型为“foursquare”的服务器调用。

找到了答案!与限制的资源有关…显然,将解Bouncems设置为0不足以阻止它执行解Bouncing部分。我切换回$resource,一切正常(但这也意味着onValueChanged的去边界部分不起作用)

        <div class="col-sm-5">
            <input class="form-control input-sm" url="type:googleplus" ng-model="element.googleplus" type="text"><br />
            <input class="form-control input-sm" url="type:yelp" ng-model="element.yelp" type="text"><br />
            <input class="form-control input-sm" url="type:foursquare" ng-model="element.foursquare" type="text>
        </div>
...
.factory('Validation', ['va', function (va) { // 'va' is a throttled resource
    var result = va('/manage/api/validate?type=:type&input=:url',
    {
        type: '@type',
        url: '@url'
    },
    {
        options: {
            method: 'options',
            debounceMS: 0, // turned off debounce in lieu of this problem
            abortPending: false
        }
    });
    return result;
}])

...
.directive('url', ['$filter', 'Validation', '$q', function ($filter, Validation, $q) {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {
            // parses some stuff
            var config = {};
            (attrs['url'] || '').split(' ').map(function (p) { var a = p.split(':'); config[a[0]] = a[1]; });
            if (config.type)
                config.type = config.type.split(',');
            config.isRequired = elm[0].required;

            function validateUrl(viewValue, type) {
                var deferred = $q.defer();
                Validation.options({ url: viewValue, type: type }, function (response) {
                    deferred.resolve(response.IsValid);
                });

                return deferred.promise;
            }

            function onValueChanged(viewValue) {
                // hits the server to check if the url is valid
                if (viewValue) {
                    var type = config.type ? config.type[0] : undefined;

                    validateUrl(viewValue, type).then(function (isValid) {
                        ctrl.$setValidity('url', isValid);
                    });

                }
                else {
                    // prevents saving the listing as field is invalid
                    ctrl.$setValidity('url', !config.isRequired);
                }

                return viewValue;
            }

            ctrl.$parsers.push(onValueChanged);

            // revalidate when the model updates
            scope.$watch(
                function () { return ctrl.$viewValue; },
                function (viewValue) { return onValueChanged(viewValue); }
            );
        }
    };
}])