我有一段代码:
$scope.getPlaylists = function() {
Restangular.all('getPlaylists').getList().then(function(getPlaylists){
var plist;
console.log("getplists: "+getPlaylists);
for(var i=0; i < getPlaylists[0].length; i++) {
plist = getPlaylists[0][i];
$scope.playlists.push(plist);
console.log("inside loop: "+$scope.playlists.length);
}
console.log("loop end: "+$scope.playlists.length);
});
console.log("total length: "+$scope.playlists.length);
};
在循环内部和循环之后,我可以在控制台中看到播放列表的长度,但total length:
被视为零。我应该怎么做以便能够以正确的值访问$scope.playlists.length
?
答案 0 :(得分:0)
您在异步调用(getList
)之后记录值 - 将日志语句放在回调中或将数据传递给回调函数并在那里完成工作。 / p>
Restangular.all('getPlaylists').getList().then(function(getPlaylists){
//all work pertaining to the info received has to be done here
//Or an external callback function
});
//Code here does not know about received data from the async call - it's still in progress