gpt4 book ai didi

javascript - 链接 $resource promise 的 Angular 问题

转载 作者:行者123 更新时间:2023-11-30 08:37:40 27 4
gpt4 key购买 nike

我试图为我拥有的这项服务链接一些 promise ,但 $stateprovider 无需等待 $q 即可解析。这导致我的 Controller 只返回空数组的结果对象,并且稍后填充该对象。我的 promise 链哪里出了问题?

我的 $stateprovider:

    .state('somestate', {
resolve: {
neededvalue: function(anotherservice) {
return anotherservice.getData(); //this one works pretty well , but doesn't use any chaining
},
finalvalue: function(myService, neededvalue) {
return myService.startChain(neededvalue);
}
}
}

我的服务:

    app.factory("myService",
['$resource', '$q',
function ($resource, $q) {

var firstApi = $resource("api/somewhere/first", {}, { "getData": { method: "POST" } });
var secondApi = $resource("api/somewhere/second", {}, { "getData": { method: "POST" } });

var result = {
array: []
};

var myService = {
startChain: startChain
};

return myService;

function startChain(someNeededValues) {
var deferred = $q.defer();

firstApi
.getData(someNeededValues.values).$promise
.then(function (data) {
result.value = data.value;
})
.then(recurringAction)
.then(deferred.resolve(result));

return deferred.promise;
}

function recurringAction() {
return secondApi.getData(result.value, function (data) {
angular.copy(data.array, result.array);
}).$promise;
}

}]);

最佳答案

当您将 deferred.resolve(result) 作为回调传递给 promise 时,不确定您的脚本是否按照预期的方式工作...

所以改变一下

                .then(deferred.resolve(result));

                .then(function(){
deferred.resolve(result)
});

关于javascript - 链接 $resource promise 的 Angular 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29945767/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com