我在php中创建了一系列卡片:
$card = array(
"id" => $row["id"],
"nome" => $row["nome"],
"indirizzo" => $row["indirizzo"],
"descrizione" => $row["descrizione"],
"prezzo" => $row["prezzo"],
"images" => array(),
"thumbs" => array(),
"dataInserimento" => $row["inserimento"],
"dataModifica" => $row["modifica"],
"bigImage" => array(),
"lat" => $row["lat"],
"lng" => $row["lng"],
"latInd"=>$row["latInd"],
"lngInd"=>$row["lngInd"]);
在循环中我做:
$cards["card"][] = $card;
我将数据传递给angular:
echo json_encode($ cards);
在控制器中,我将数组复制到范围变量中:
$scope.cards=[];
CardService.getCards($scope.lastCard).then(function (success) {
CardService.setLocalCards(success.data.card);
/*check if all imges are loaded*/
for (var i = 0; i < success.data.card.length; i++) {
console.log(success.data.card);
console.log("numero di card caricate " + success.data.card[i].id);
var localCard = CardService.getLocalCard(success.data.card[i].id);
if (localCard === null || localCard.photosLoaded === true) {
$scope.cards.push((success.data.card[i]));
$scope.lastCard = success.data.card[i].id;
}
}
$ionicLoading.hide();
console.log("ultima scheda caricata " + $scope.lastCard);
}, function (fail) {
console.log("getCards error " + JSON.stringify(fail));
});
console.log($scope.cards);
问题是,如果我尝试访问$ scope.cards中的元素,我可以从CardService中的then函数访问它们,但在外面我得到这个:
答案 0 :(得分:1)
由于您正在使用异步的promises,因此您的console.log正在CardService.getCards
函数之后和.then()
内的函数之前执行。 .then中的函数是仅在getCards
完成后才能执行的承诺。所以,当你console.log($scope.cards);
到达时,它仍然是空的。
无论哪种方式,请确保您实际上在$scope.cards
内进行任何推送,并且您可以尝试在承诺内部执行console.log($scope.cards)
(推牌后),看看它是否真的填充。