繁体   English   中英

无法使用Angular JS访问$ scope变量

[英]Cannot access $scope variable with angular js

我有一段代码:

$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

您正在异步调用( getList )之后记录值-将log语句放在回调中或将数据传递给回调函数并在其中进行工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM