2017-03-13 7 views
1

제목에서 알 수 있듯이 참조 스위칭에 문제가 있습니다. 내 HTML : 내 JS 코드에서Ng-repeat not updating array

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

, 나는 두 가지를 할 노력하고있어. 첫 번째 단계는 서버에서 데이터를 가져와 어레이 (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(); 
}]); 

그래서 무슨 일이 일어나고 있는지 나는 allsongs에 반복을 겨 있다면, 나는 않은 구문 분석 URL을 잔뜩받을 수 있나요 있다는 것입니다. 그러나 만약 내가 parseurl을 반복한다면, 나는 아무것도 얻지 못한다. 분명히 참조가 바뀌지는 않지만 어떻게 할 수 있습니까?

+0

'console.log ($ scope.parseurl)'을하고 확인 했습니까? – Pradeepb

+0

데이터가 유효한지 확인하기 위해 var 객체에 중단 점을 넣습니다. 나는 객체를 parseurl로 밀어 넣어 배열을 갱신 할 것이라고 가정한다. – ellusion

+0

중복이 있고 그 다음에 수십 plicates가있다. – jcaron

답변

2

$scope.parser()은 데이터를받은 후에 호출해야합니다. 다음 예제와 같이 약속 콜백 함수에 넣으십시오. $http은 비동기 함수입니다. 그러면 요청이 완료되기 전에 $scope.parser()이 실행되었습니다.

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