2014-11-17 2 views
0

각도 서비스는 http 게시를 수행하는 동안 컨트롤러의 데이터를 반환하지 않습니다.HTTP 서비스를 수행하는 동안 각도 서비스가 컨트롤러의 데이터를 반환하지 않습니다.

app.controller("letter_active", function ($scope, myService) { 
    $scope.artist = myService.getReplyType2(); 
}); 

app.service('myService', function ($http) { 

    this.getReplyType2 = function() { 
     $http.post('file/getReplyType', {}) 
     .success(function (data, status) { 
      return data; 
     }).error(function (data, status) { 
      alert("Error has occured" + status); 
     }); 
    } 
}); 

이 나를 초보자를위한 도움이 필요 각도

+0

당신은 사람들이 당신에게 –

답변

1

this.getReplyType2 = function() { 
    var defer = $q.defer(); 
     $http({method: 'POST', url: 'file/getReplyType', data: {}}) 
      .success(function(data, status, headers, config) { 
       // this callback will be called asynchronously 
       // when the response is available 
       defer.resolve(data); 
        }) 
      .error(function(data, status, headers, config) { 
       // called asynchronously if an error occurs 
       // or server returns response with an error status. 
       window.data = data; 
      }); 

    return defer.promise; 
} 

app.controller("letter_active", function ($scope, myService) { 
    myService.getReplyType2().then(function(data) { 
     $scope.artist = data;  
    }); 
}); 
+0

감사합니다 ... – user2601893

0

각도에서 비동기 서비스 전형적인 작품은 서비스가 약속 개체를 반환하고, 컨트롤러에이 범위의 데이터를 설정하려면이 약속의 then/success 방법을 사용하는 것입니다 :

app.controller("letter_active", function ($scope, myService) { 
    myService.getReplyType2().then(function(data) { 
     $scope.artist = data;  
    }); 
}); 

app.service('myService', function ($http) { 

    this.getReplyType2 = function() { 
     return $http.post('file/getReplyType', {}).success(function (data, status) { 
      return data; 
     }).error(function (data, status) { 
      alert("Error has occured" + status); 
     }); 
    } 
}); 

참고, 그 getReplyType2 방법은 이제 약속 개체입니다 $http.post 호출의 결과를 반환합니다. 당신이 약속을 반환해야 비동기 호출의

+0

반환 데이터를 돕기 위해 자신의 일에서 시간이 걸릴 것으로 예상되는 경우 적어도 문법 및 문장을 확인하십시오; <<이 줄을 더 잘 삭제 하시길 바랍니다. –

+0

@PetrAveryanov 왜? – dfsq

+0

@ user2601893 그것은'myService.getReplyType2(). then'이어야합니다. 오타라고 생각합니다. – dfsq

관련 문제