Angularjs 如何在服务中存储RESTAPI结果以避免重新查询
服务 控制器:Angularjs 如何在服务中存储RESTAPI结果以避免重新查询,angularjs,rest,Angularjs,Rest,服务 控制器: app.factory('Notes', ['$resource', function($resource) { return $resource('/notes/:id', {id: '@id'}, { 'query': {method: 'GET', params: { format: 'json'}, isArray: true}, 'update': {method:'PUT'},
app.factory('Notes', ['$resource', function($resource) {
return $resource('/notes/:id', {id: '@id'},
{
'query': {method: 'GET', params: { format: 'json'}, isArray: true},
'update': {method:'PUT'},
'save': {method:'POST'},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
},
{ stripTrailingSlashes: false}
);
}]);
如果我们在多控制器中使用Notes服务,它将一次又一次地获取数据
如何避免获取数据?我只想获取一次数据,然后向每个控制器提供相同的数据。您尝试过缓存选项吗?问题取决于您是否更新数据。在web sercheasy way$rootScope或Angular Cookie或LocalStorage@charlietfl,谢谢你的回复。我尝试了缓存选项。但当我添加/更新数据时,它似乎不起作用。
app.controller('NotesCtrl', ['$scope', '$routeParams', 'Notes',
function($scope, $routeParams, Notes) {
$scope.notes = Notes.query();
Notes.query(
function(data) {
$scope.notes = data;
// success handler
},
function(error) {
alert('error')
// error handler
}
);
}]);