Tôi có bộ điều khiển và nhà máy được xác định như bên dưới.Angular js trả về đối tượng không xác định từ nhà máy
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;
}
});
}
};
});
Điều làm tôi bối rối là đầu ra của bảng điều khiển của tôi là dòng liệt kê các đối tượng từ nhà máy của tôi. Tôi cũng đã cố gắng thay đổi điều khiển của tôi để
myApp.controller('ListController',
function($scope, ListFactory) {
ListFactory.get().then(function(data) {
$scope.posts = data;
});
console.log($scope.posts);
});
Nhưng tôi nhận được lỗi
TypeError: Cannot call method 'then' of undefined
Lưu ý: Tôi tìm thấy thông tin này về việc sử dụng một nhà máy thông qua http://www.benlesh.com/2013/02/angularjs-creating-service-with-http.html