嗨我有两个控制器
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发出请求并接收数据,但控制器中的所有项都是空的.