2013-07-03 5 views
2

서버에서 JSON 응답을 읽으려고합니다. 내 코드는 여기에서 찾을 수 있습니다. https://github.com/ameyjah/feeder모듈 팩토리가 데이터를 반환하지 않습니다.

파이어 폭스 파이어 버그에서 서버가 JSON 응답을 반환했지만 $ scope.variable에 저장할 때 해당 정보에 액세스 할 수 없다는 것을 알 수 있습니다.

모듈 코드 var에 고해상도

$scope.sites = feedFetcher.sites().sites; 
console.log($scope.sites); 

답글 불을 지르고에서 볼

angular.module('myApp.services', ['ngResource']) 
    .factory('feedFetcher', ['$resource', 
     function($resource) { 
      var actions = { 
       'sites': {method:'GET', params: { action:"sites"} ,isArray:false},       
       'feeds': {method:'GET', params: { action:"sites"} ,isArray:false} 
      } 
      res = $resource('/api/:action', {}, actions); 
      return res 
     } 
    ]); 

컨트롤러 코드 : 나는 내 공장을 정의해야하는 방법을 엉망 생각

{ 
    "sites": [ 
    { 
     "id": 0, 
     "title": "google" 
    }, 
    { 
     "id": 1, 
     "title": "yahoo" 
    } 
    ] 
} 

그러나 나는 확인할 수 없다. 어떤 도움이 도움이 될 것입니다.

+0

정의 사이트 및 피드가 동일합니까? –

+0

@JeffFoster 오 그냥 복사 붙여 넣기 오류이지만 사이트() 여전히 데이터를 반환하지 않습니다. –

답변

1

sites()를 호출하면 빈 객체가 반환되고 AJAX 요청이 시작되어 해당 객체의 "sites"속성이 채워집니다.

$scope.results = feedFetcher.sites(); 
console.log($scope.results.sites); // will be undefined as AJAX not complete 

그런 다음 HTML에서 당신은 결과를 사용할 수 있습니다 그리고 그들은 AJAX가 완료 될 때 채울 것인지, 또는 그것을보고 : 나는 당신이 이런 식으로 사용한다고 생각 boths의

$scope.$watch('results.sites', function() { 
    // this will be called twice, once initially 
    // and again whenever it is changed, aka when AJAX completes 
    console.log($scope.results.sites); 
}; 
관련 문제