以下のように定義されたコントローラーとファクトリーがあります。
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