首页 文章

控制器调用上的角度填充和返回服务var

提问于
浏览
0

我正在努力解决这个问题,并且无法弄明白 . 我所拥有的是主控制器,工厂和服务,我正在尝试将数组从服务存储到控制器中的$ scope . 在项目视图上单击此控制器中的此功能被触发:

$scope.getSongsInPlaylist = function()
                    {
                        var playlistId = this.item.idPlayliste;                        
                        $scope.Mp3Files = songInPlaylistFactory.getSongsForPlaylist(playlistId);

                    }

这没关系,这个函数从视图中检索项目,并将该项目的id发送到服务中 . 在我的服务中,我有这样的代码:

var Songs = [ ];
    this.getSongsForPlaylist = function (id)
                        {
                             for (var i = 0; i < SongIsOnPlaylist.length; i++)
                             {
                                 if(SongIsOnPlaylist[i].idPlayliste == id)
                                 {                                 
                                     dataFactory.getDataById(mp3fileUrl, SongIsOnPlaylist[i].idPjesme)
                                     .success(function (data) {
                                         Songs.push(data);
                                         alert(Songs[0].naslovPjesme);//Alert one                              
                                     });                           
                                 }                            
                             }
                             alert(Songs[0]);// Alert two
                             return Songs;                         
                         }

dataFactory是我的工厂,在后端与api通信,这也有效 . var Songs定义为:var Songs = []; SongIsOnPlaylist充满了数据 .

当我触发这个时,警报二给我未定义,警报一个给了我歌曲中第一首歌的名字 . 这意味着var Songs充满了数据,但当我希望它返回控制器时它的空...

我在这里做错了什么,我会感激任何帮助吗?

1 回答

  • 1

    首先看起来你的 dataFactory.getDataById 是异步调用 . 因此,实际发生的是在所有异步调用返回时填充之前返回一个空的 Songs 数组 .

    要解决这个问题,我建议使用像bluebird这样的Promise库来做这样的事情:

    // note that now your service will return a promise
    this.getSongsForPlaylist = function (id) {
      return new Promise(function(resolve, reject) {
    
        var promises = [];
        // here in your loop just populate an array with your promises
        for (var i = 0; i < SongIsOnPlaylist.length; i++){
          if(SongIsOnPlaylist[i].idPlayliste == id){                                 
            promises.push( dataFactory.getDataById(mp3fileUrl, SongIsOnPlaylist[i].idPjesme) )
          }                            
        }
        // now use the library to resolve all promises  
        Promise.all(promises).then( function (results) {
          //HERE YOU WILL GET the results off all your async calls
          // parse the results 
          // prepare the array with songs 
          // and call
          resolve(Songs); 
        });
      });
    
    }
    

    然后你将使用这样的服务:

    $scope.getSongsInPlaylist = function() {
      var playlistId = this.item.idPlayliste;                        
      songInPlaylistFactory.getSongsForPlaylist(playlistId)
      .then(function(Songs){
        $scope.Mp3Files = Songs
      })
      .error(function(err){
        //here handle any error
      });
    }
    

相关问题