2014-10-28 3 views
0

서비스가 종속 된 단순 컨트롤러가 있습니다. 일단 resource의 약속이 해결되면 범위를 채우려고합니다. 내가 console.log() 범위로 표시되면 데이터 범위가 올바르게 표시되지만 ng-repeat는 작동하지 않습니다. 또한 각도 확장 chrome을 사용 중이며 해당 범위 (posts)에 데이터가 표시되지 않습니다. 내가 빠진 게 뭐야?각도 리소스가 범위를 채우지 않습니다.

컨트롤러 :

function TimelineController($scope,TimelineService) 
    { 
     $scope.posts = []; 

     $scope.getPosts = function(wall_type,wall_id) { 
      TimelineService.getPosts(wall_type,wall_id).$promise.then(function(result){ 
       $scope.posts = result.response; 
       console.log($scope.posts); // it show correctly the data on console 
      }); 
     } 
    } 

서비스

function TimelineService($resource) { 

     var resource = $resource('/timeline/:postId', 
      {postId:"@id"} 
     ); 

     return { 
      getPosts: function(entity,id) 
      { 
       // /timeline?entity=user&id=3 (example url) 
       return resource.get({entity:entity,id:id}); 
      } 
     } 
    }; 

서버의 응답은 다음과 같습니다

{success:true, response:[...]} 

편집 :

나는 함께 템플릿을 표시하는 지시어를 사용하고 ng-repeat에서 :

<div class="col-md-8" ng-controller="TimelineController"> 
<timeline type="user" ids="8" postime="getPosts(wall_type,wall_id)"></timeline> 
</div> 

그리고 내 템플릿의 짧은 버전 :

<ol class="timeline" ng-init="postime({wall_type:type,wall_id:ids})"> 
    <li ng-repeat="post in posts"> 
     {{post.poster.fullname}} 
    </li> 
</ol> 
+0

보기는 어디에 있습니까? 특히'ng-repeat' 언급 됨 – Cherniv

+0

죄송합니다. 내 게시물을 편집하십시오. – Fabrizio

답변

0

나는 내보기에 그래서

function timeline() { 

     return { 
      restrict: 'E', 
      replace: true, 
      scope: { 
       type: '@', 
       ids: '@', 
       postime: '&' 
      }, 
      templateUrl:"/js/templates/timeline/post-tmpl.html" 
     } 
}; 

서비스 기능이 컨트롤러 범위의 일부가 아니기 때문에 데이터를 적용하고 소화해야한다고 생각했습니다.

컨트롤러 내에서 console.log 이전 또는 이후에 $scope.$apply()을 실행하십시오. $ 적용하거나 $ 소화가 이미 진행중인 경우

은 당신은 0의 지연과의 setTimeout으로 $scope.$apply()을 포장 할 수 있습니다, 또는 당신은 $scope.$evalAsync()로 포장 할 수 있습니다.

+0

답변을 주셔서 감사합니다.하지만'$ scope. $ apply()'를 추가하면 다음 오류 메시지가 나타납니다 : https://docs.angularjs.org/error/$rootScope/ inprog? p0 = $ digest하지만 최소한 데이터는 '각도 크롬 검사기'에만 표시됩니다. – Fabrizio

+0

답변을 편집하여 해당 문제에 대한 옵션을 해결할 수 있습니다. – James

+0

고마워,하지만 컨트롤러의 범위를 지시문에 사용할 수없는 몇 가지 이유로 때문에 문제를 발견했습니다, 그래서 지시문 내에서 새 매개 변수로 범위를 전달할했습니다 – Fabrizio

관련 문제