2017-03-13 39 views
1

正如標題所說,我有一個參考切換的問題。 我的html:吳重複不更新陣列

div ng-repeat="data in parseurl"> 
{{data.url}} 
</div> 

在我的JS代碼中,我試圖做兩件事。第一步是從服務器獲取數據並將其放入一個數組(稱爲allsongs)。之後,我解析數據並將其放入另一個數組(parseurl)。

var app = angular.module("write", []); 
app.controller("Ctrl", ['$scope', '$http', function($scope, $http){ 
    $scope.allsongs = []; 
    $scope.parseurl = []; 
    $scope.getstuff = function(){ 
      $http.get("my link here").then(function(response){ 
       $scope.allsongs = response.data; 
       }); //step one -> this works! 

    $scope.parser(); //step two 
    }; 

    $scope.parser = function() 
    { 
    for(i=0;i<$scope.allsongs.length;i++) { 
    for(var key in $scope.allsongs[i]) { 
     var value = $scope.allsongs[i][key]; 
     var object = {Url : value.Url}; //this does contain the correct info I want 
     $scope.parseurl.push(object); 
    } 

$scope.getstuff(); 
}]); 

所以發生的是,如果我NG-重複上allsongs,然後我得到了一堆未解析的網址。但是如果我在parseurl上重複,那麼我什麼也得不到。顯然這個參考文獻沒有改變,但我該怎麼做?

+0

你做了'console.log($ scope.parseurl)'並檢查? – Pradeepb

+0

我把一個斷點放在var對象上,來檢查數據是否有效。我認爲將對象推入parseurl將更新數組 – ellusion

+0

有重複,然後有數百萬個重複。 – jcaron

回答

2

$scope.parser()需要在數據被接收後調用。將它放入您的承諾回調函數中,如下例所示。請注意0​​是一個異步功能。那樣$scope.parser()在你的請求完成之前被執行。

$scope.getstuff = function(){ 
    $http.get("my link here").then(function(response){ 
     $scope.allsongs = response.data; 
     $scope.parser(); 
    }); 
};