2013-05-13 1 views
0

다음 작업을 수행해야하는 메시징 서비스를 만들고 있습니다. 1. Google의 메시지 서비스에서 메시지를로드하고 수신자의 ID를 가져온 다음 수신자의 정보를로드합니다. 사용자 서비스 메시지 서비스 콜백을 사용하고 메시지 객체에 대한 감시자를 많이 만들지 않고 시도했습니다. 서비스는 작동하지만 결과를 $ 범위에 올바르게 할당하지 않습니다. 여기 컨트롤러가 있습니다. 모든 서비스는 제대로 작동 :

function MessageCtrl ($scope, $http, $location, $routeParams, Messages, Members) { 
    if ($routeParams.mid) { // Checks for the id of the message in the route. Otherwise, creates a new message. 
     $scope.mid = $routeParams.mid; 
     $scope.message = Messages.messages({mid: $scope.mid}).query(); 

     $scope.$watch("message", function (newVal, oldVal, scope) { 
      if (newVal.data) { 

       $scope.recipients = Members.members({uids: newVal.data[0].uids}).query(); 
      } 
     }, true); 

    } else { 
     $scope.create = true; 
    } 

    // Events 

    $scope.save = function() { }; 
    $scope.preview = function() { }; 
    $scope.send = function() { }; 
} 

답변

1

query를 사용하는 올바른 방법은 쿼리 기능에 전달되는 콜백에서 작업을 수행하는 것입니다. 즉, $scope.message을 콜백에 할당해야합니다. 또한 $ 시계가 필요하지 않습니다. 콜백 내에서 다른 서비스를 직접 호출 할 수 있습니다. 그러나 깨끗한 deferred

http://docs.angularjs.org/api/ngResource 사용하십시오 유지합니다. $ 자원

http://docs.angularjs.org/api/ng. $ q를

관련 문제