我怎么能叫在angularjs循环第二顺序的承诺设计模式的方法? [英] How can I call the second sequential promise design pattern method in a loop in angularjs?
问题描述
新建angularjs和尝试的承诺模式的第一次 -
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是所有secondAttributes为$ scope.myobjectArray的所有对象级联阵列。
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.
编辑:
当我试图访问$ scope.myObjectArray [J]。'然后'函数里,故称$ scope.myObjectArray [J]是不确定的。 - >所以我创建了一个currentType可变的,并且$ scope.myObjectArray [J]。它和那是',那么'函数内部很方便。奇怪!
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
任何帮助是AP preciated。
Any help is appreciated.
var myObject = function(firstattribute, secondAttribute){
this.firstattribute = firstattribute;
this.secondAttribute = secondAttribute;
}
由蜂巢( 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是最后一个,因为对于j循环已经结束。所以,你需要做的是移动code从 utility.getData code>开始传递
currentType $的参数,一个单独的方法C $ C>和
seccondAttributeArray
这样的关闭将包含它们作为函数参数,而不是改变他们的对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屋!