【发布时间】:2013-12-11 06:51:09
【问题描述】:
您好,我有两个控制器
pqsAppModule.controller('NotificationBoxController',function($scope,items) {
$scope.list = items.list();
})
和
pqsAppModule.controller('NotificationController',function($scope,items) {
$scope.list = items.list();
})
我需要创建一个“项目”服务,该服务将执行 ajax 请求并为任何将其注入的控制器返回数据。而且我希望查询只执行一次,并且项目将在所有控制器之间共享。
pqsAppModule.factory('items', function($http) {
var items = [];
var itemsService = {};
$http.get('api/notification').then(function(response){
items = response.data;
});
itemsService.list = function() {
return items;
};
return itemsService;
});
但我不明白为什么 Angular 发出请求并接收数据,但控制器中的所有项目都是空的。
【问题讨论】:
标签: angularjs