admin管理员组文章数量:1392003
Not sure if this is the best way to state the question but I only want the resolve to run once when I change the state. This is because I have multiple tabs and I need to use an AJAX call to get the data for the template. So when you click on a tab, I will store that information into the $scope. But I don't want to keep making this call if the user switches to another tab and goes back.
This is what my $stateProvider looks like right now:
state('something', {
views: {
'filters.form': {
templateUrl: '<%= asset_path('my_path.html') %>',
controller: function($scope, sources){
$scope.sources = sources;
}
}
},
resolve: {
sources: function($http) {
return $http.get('/sources').success(function(data){
return JSON.parse(data.sources);
});
}
}
})
Which works great but everytime I switch tabs and e back, it makes another call which I don't want.
I tried to pass in the $scope into the resolve and checked to see if $scope.sources existed before I made an AJAX call but that didn't work.
Not sure if this is the best way to state the question but I only want the resolve to run once when I change the state. This is because I have multiple tabs and I need to use an AJAX call to get the data for the template. So when you click on a tab, I will store that information into the $scope. But I don't want to keep making this call if the user switches to another tab and goes back.
This is what my $stateProvider looks like right now:
state('something', {
views: {
'filters.form': {
templateUrl: '<%= asset_path('my_path.html') %>',
controller: function($scope, sources){
$scope.sources = sources;
}
}
},
resolve: {
sources: function($http) {
return $http.get('/sources').success(function(data){
return JSON.parse(data.sources);
});
}
}
})
Which works great but everytime I switch tabs and e back, it makes another call which I don't want.
I tried to pass in the $scope into the resolve and checked to see if $scope.sources existed before I made an AJAX call but that didn't work.
Share Improve this question asked Aug 21, 2014 at 20:49 DragonflyDragonfly 4,3617 gold badges35 silver badges60 bronze badges 1- I would move it to a service and have the service do the caching. – Kevin B Commented Aug 21, 2014 at 20:53
1 Answer
Reset to default 6I would just move it in a service and cache it and inject it in the resolve.
app.service('SourceService',['$http', function(){
var _cachedPromise;
this.getSources = function(){
return _cachedPromise || _cachedPromise = $http.get('/sources').then(function(result){
//return result.data.sources //If the data is already an object which most possibly is just do
return JSON.parse(result.data.sources);
});
}
}]);
Now in the resolve do:-
resolve: {
sources: function(SourceService) {
return SourceService.getSources();
}
}
or just do it in the resolve itself.
本文标签: javascriptHow to cache AngularJS uirouter resolveStack Overflow
版权声明:本文标题:javascript - How to cache AngularJS ui-router resolve? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744759560a2623671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论