我想问一下,使用"回调"是否可能(以及如何)使用promise在Angular中for for循环?
这意味着:
$scope.changeToBigImages = function() {
for(var productsLinkAli in $scope.productImagesThumbs.aliexpress) {
$scope.convertToBigSrc($scope.productImagesThumbs.aliexpress[productsLinkAli], URL_TYPE_ALIEXPRESS);
}
for(var productsLinkEbay in $scope.productImagesThumbs.ebay) {
$scope.convertToBigSrc($scope.productImagesThumbs.ebay[productsLinkEbay], URL_TYPE_EBAY);
}
//If 1.st a 2.nd for loop is finished, next action should be triggered
}
$scope.convertToBigSrc = function(LINK_THUMB, TYPE) {
var bigImage;
console.log(LINK_THUMB);
console.log(TYPE);
switch (TYPE) {
case URL_TYPE_EBAY:
var replacedImgs = [];
for(var productsLinkEbay in LINK_THUMB) {
bigImage = LINK_THUMB[productsLinkEbay].replace(IMG_PATH_EBAY_FIND, IMG_PATH_EBAY_REPLACE); //http://i.ebayimg.com/images/g/uSYAAOSw7FRWZIvs/s-l64.jpg
replacedImgs.push(bigImage);
}
$scope.productImagesBig.ebay.push(replacedImgs);
break;
case URL_TYPE_ALIEXPRESS:
var replacedImgs = [];
for(var productsLinkAli in LINK_THUMB) {
bigImage = LINK_THUMB[productsLinkAli].replace(IMG_PATH_ALI_FIND, IMG_PATH_ALI_REPLACE); //http://i.ebayimg.com/images/g/uSYAAOSw7FRWZIvs/s-l64.jpg
replacedImgs.push(bigImage);
}
$scope.productImagesBig.aliexpress.push(replacedImgs);
break;
}
}
我知道我可以在for循环中观察索引的值,但我想知道使用promises这样的东西是可能的(也是最佳的)。
非常感谢您的任何建议。