我想要使用AngularJS承诺/然后递归函数。但是没有调用then函数(调用错误,成功,通知回调函数都没有)。AngularJS,承诺与递归功能
这里是我的代码:
递归函数
loadSection2 = function() {
var apiURL = "http://..."
var deferred = $q.defer();
$http({
method: "GET",
url: apiURL
}).success(function(result, status, headers, config) {
console.log(result);
loadCount++;
if(loadCount < 10) {
newSectionArray.push(result);
loadSection2();
} else {
loadCount = 0;
deferred.resolve();
return deferred.promise;
}
}).error(function() {
return deferred.reject();
});
deferred.notify();
return deferred.promise;
};
然后
loadSection2().then(function() {
console.log("NEW SECTIONS LOADED, start adding to document");
addContent();
}, function() {
console.log("ERROR CALLBACK");
}, function() {
console.log("NOTIFY CALLBACK");
}).then(function() {
loadScrollActive = false;
});
我认为,当时有至少拿到第一通知回调。但没有回调。 然后不使用递归函数?
你能给我们一个jsfiddle吗? – Manishearth
我看到的一件事是你不能从回调函数中返回一些东西。所以在.success和.error中返回deferred.promise实际上什么都不做。虽然不是问题的原因。 – Narretz
'loadCount'在哪里定义?而且,“通知”不像你想象的那样工作。我有一个开放的问题,在角度回购 - > https://github.com/angular/angular.js/issues/5277 – Rifat