Dra*_*fly 5 javascript jquery angularjs angular-ui-router
不确定这是否是陈述问题的最佳方式,但我只希望在更改状态时运行一次.这是因为我有多个选项卡,我需要使用AJAX调用来获取模板的数据.因此,当您单击选项卡时,我会将该信息存储到$ scope中.但是,如果用户切换到另一个选项卡并返回,我不想继续拨打此电话.
这就是我的$ stateProvider现在的样子:
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);
});
}
}
})
Run Code Online (Sandbox Code Playgroud)
哪个效果很好,但每次切换标签然后回来时,都会拨打另一个我不想要的电话.
我试图将$ scope传入解析并检查$ scope.sources是否在我进行AJAX调用之前存在但是没有用.
我只是将它移动到一个服务中并缓存它并将其注入解析中.
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);
});
}
}]);
Run Code Online (Sandbox Code Playgroud)
现在在决心做: -
resolve: {
sources: function(SourceService) {
return SourceService.getSources();
}
}
Run Code Online (Sandbox Code Playgroud)
或者只是在解决方案中做到这一点.
| 归档时间: |
|
| 查看次数: |
4114 次 |
| 最近记录: |