컨트롤러와 공장은 다음과 같이 정의되어 있습니다. 나를 혼란 무엇공장에서 정의되지 않은 객체를 반환하는 각도 js
myApp.controller('ListController',
function($scope, ListFactory) {
$scope.posts = ListFactory.get();
console.log($scope.posts);
});
myApp.factory('ListFactory', function($http) {
return {
get: function() {
$http.get('http://example.com/list').then(function(response) {
if (response.data.error) {
return null;
}
else {
console.log(response.data);
return response.data;
}
});
}
};
});
내가 내 컨트롤러에서 정의되지 않은 출력을 얻을 다음 콘솔 출력의 다음 행 내 공장에서 객체의 내 목록이 때문이다. 또한
myApp.controller('ListController',
function($scope, ListFactory) {
ListFactory.get().then(function(data) {
$scope.posts = data;
});
console.log($scope.posts);
});
내 컨트롤러를 변경 시도하지만 오류를
TypeError: Cannot call method 'then' of undefined
참고가 나타날 나는 http://www.benlesh.com/2013/02/angularjs-creating-service-with-http.html 당신은 콜백을 사용하거나 필요