如何在 angularjs 的循环中调用第二个顺序承诺设计模式方法? [英] How can I call the second sequential promise design pattern method in a loop in angularjs?
问题描述
刚接触 angularjs 并第一次尝试 promise 模式 -
New to angularjs and trying out the promise pattern for the first time -
我有一个服务实用程序,里面有这个方法 -
I have a service utility inside which I have this method -
this.getData= function(url){
var defer = $q.defer();
$http({method: 'GET', url: url}).
success(function(data, status){
defer.resolve(data);
})
.error(function(data, status) {
defer.reject(status);
});
return defer.promise;
};
现在在我的控制器中,我正在调用一个名为 A() 的方法
Now inside my controller, I am calling a method called A()
var A = function () {
$scope.myobjectArray = [];
return utility.getData("some url").then(funciton(data)
{
for (i = 0; i < data.length; i++) {
$scope.myobjectArray.push(data[i].attribute1, new Array());
}
}
).
then(function () {
return getTheSecondAttributeArray();
}).catch(function (status) {
//display error
});
};
var getTheSecondAttributeArray = function () {
for (i = 0; i < $scope.myObjectArray.length; i++) {
var secondAttributeArray = [];
var currentType = $scope.myObjectArray[i];
utility.getData("some url").then(function (response) {
for (j = 0; j < response.length; j++) {
//some response manipulation
secondAttributeArray.push(response[j].text);
}
currentType.secondAttribute = secondAttributeArray;
}).catch(function () {//catch error, display message
})
}
}
但是,看起来 $scope.myobjectArray 的最后一个元素(第 n-1 个元素)只会被填充.此外,最后一个元素包含的 secondAttributeArray 是 $scope.myobjectArray 的所有对象的所有 secondAttributes 的串联数组.
However, it looks like that the last element of the $scope.myobjectArray (n-1th element) is only getting populated. Also, the secondAttributeArray that this last element contains is a concatenated array of all secondAttributes for all objects of the $scope.myobjectArray.
不知道我可以在这里更改什么.
Cannot figure out what can I change here.
当我尝试在then"函数中访问 $scope.myObjectArray[j] 时,它说 $scope.myObjectArray[j] 未定义.--> 所以我创建了一个 currentType 变量并将 $scope.myObjectArray[j] 分配给它,这在then"函数中很容易访问.奇怪!
When I tried accessing $scope.myObjectArray[j] inside the 'then' function, it said $scope.myObjectArray[j] was undefined. --> And so I created a currentType variable and assigned $scope.myObjectArray[j] to it and that was easily accessible inside the 'then' function. Weird!
另外,我看到只有 $scope.myObjectArray 的最后一个对象获取值,其余的没有.数组中的其余对象为空
Also, I see that only the last object of the $scope.myObjectArray gets values and not the rest. The rest of the objects in the array are empty
感谢任何帮助.
var myObject = function(firstattribute, secondAttribute){
this.firstattribute = firstattribute;
this.secondAttribute = secondAttribute;
}
Beehive 的解释 (Angularjs $q.all) 是我面临的问题.我只得到最后一个循环的数据.
The explanation here by Beehive (Angularjs $q.all) is something that I am facing. I only get the last loop's data.
推荐答案
问题全在于函数闭包.当获取数据返回时,您的 currentType 是最后一个,因为 for j 循环已经结束.所以你需要做的是将代码从 utility.getData
移动到一个单独的方法,传递 currentType
和 seccondAttributeArray
的参数,所以闭包将包含它们作为函数的参数,并且不会随着 for j 循环的进行而改变它们.
The issue is all in function closures. When the get data returns, your currentType is the last one because the for j loop ended already. So what you need to do is move the code starting from utility.getData
to a separate method passing the parameters of the currentType
and the seccondAttributeArray
so the closure will contain them as parameters of the function and not change them as the for j loop progresses.
for (i = 0; i < $scope.myObjectArray.length; i++) {
var secondAttributeArray = [];
var currentType = $scope.myObjectArray[i];
fillSecondAttributeArray($scope, secondAttributeArray, currentType);
}
这篇关于如何在 angularjs 的循环中调用第二个顺序承诺设计模式方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!