2014-01-16 3 views
0

내가 갖고있는 편안한 서비스를 호출하고 내 컨트롤러의 데이터를 채워서 JVector 맵에 데이터를 채울 수 있습니다. 내가 잘못된 접근법을 취할 가능성이 높습니다 (지시어를 사용해야 할 수도 있습니다). 서비스가 정상적으로 작동하고 예상대로 데이터를 다시 얻고 있음을 알고 있습니다. 그러나 console.log를 통해 json 내의 데이터를 검사 할 때 정의되지 않은 상태가됩니다. 나는 그것이 약속을 되찾고 있기 때문에 데이터가 실제로 페이지에 표시 될 때까지 실제로 채워지지 않기 때문이라고 나는 믿는다. 나는 틀린 일을하는 것이 가장 가능성이 높습니다.AngularJS 리소스 - 컨트롤러 내부에 스코프 데이터 채우기

의견을 보내 주시면 대단히 감사하겠습니다.

모듈 :

var app = angular.module('app', ['ngResource']). 
     factory('mapDataSvc', function ($resource) { 
      return $resource('api/sites', {}); 
     }); 

컨트롤러 :

var mapCtrl = function ($scope, mapDataSvc) { 
    //http://coder1.com/articles/consuming-rest-services-angularjs 
    $scope.mapData = {}; 

    mapDataSvc.query(function (response) { 
     // Assign the response INSIDE the callback 
     $scope.mapData = response; 
    }); 

//I see the sites array in this log output 
console.log($scope.mapData); 

//This is showing undefined. 
    console.log($scope.mapData.Sites); 

} 

예 JSON :

{ 
    Sites: [{ 
     VendorName: "MyVendor", 
     SystemHardwareId: 111111, 
     Longitude: 45.22, 
     Longitude: -71.0418 
    }] 
} 
+0

당신 말은 코멘트에서 "나는이 로그 출력에 사이트 배열을 참조". 하지만 거기에 배열이 보이지 않아야합니다. 배열을 보유하고있는 Sites 속성을보아야합니다. 이 점을 분명히 할 수 있습니까? – idursun

+0

당신은 올바른 idursun .... 그것은 데이터의 배열을 보유하고 사이트 속성입니다 – scarpacci

답변

0

이 mapData이 mapDataSvc.query 내부 초기화 전에 CONSOLE.LOG이 실행되는 것으로 보인다. 이 같은 mapDataSvc.query로 이동하려고 :

var mapCtrl = function ($scope, mapDataSvc) { 
//http://coder1.com/articles/consuming-rest-services-angularjs 
$scope.mapData = {}; 

mapDataSvc.query(function (response) { 
    // Assign the response INSIDE the callback 
    $scope.mapData = response; 

    //I see the sites array in this log output 
    console.log($scope.mapData); 

    //Should be not undefined. 
    console.log($scope.mapData.Sites); 
}); 

}

관련 문제