$ http.get의 데이터를 내 컨트롤러의 변수에 할당하려고합니다.
$http.get(URL).success(function (data) {
$scope.results = data;
console.log('results in $http.get :'+ $scope.results);
});
console.log('results after http.get'+ $scope.results);
첫 번째 콘솔 로그 인쇄 데이터는 get입니다. $ http.get (url) .success 후 $ scope.results는 정의되지 않은 것으로 인쇄합니다.
다른 답변은 메서드가 비동기이므로 즉시 반환됩니다. 성공 콜백은 변수를 채우고 완료 할 때 호출되며, 바인딩을 올바르게 설정하면 UI가 업데이트됩니다. 그것은 꼭해야만하는 것처럼 행동합니다. –
이제 다음과 같은 데이터를 얻으려고합니다. var getSomething = function() { return $ http ({method : "GET", url : URL}) then (function (result) { return result.data; }}); }; 결과를 변수에 할당합니다. $ scope.results = getSomething(); – hwg