【发布时间】:2016-02-04 14:39:36
【问题描述】:
我有这个控制器:
class Ctrl{
constructor($http, $scope, $stateParams){
if(!$scope.items){
$http.get(...).then((res)=> {
$scope.items = res.data;
});
}
if($stateParams.id){
$scope.currItem = $scope.items[$statePrams.id];
}
}
我有两种状态:
.state('dashboard.items', {
url: '/items',
templateUrl: 'items.html',
controller:'Ctrl'
})
.state('dashboard.items.details', {
url: '/:id',
templateUrl: '/ItemDetails.html',
controller: 'Ctrl'
})
在显示其中一个的详细信息之前,我想确保 items 数组已经初始化。
如何不重复代码? (如果 else 和每个内部的代码相同)
谢谢。
【问题讨论】:
-
在
.state对象文字中使用resolve方法来延迟控制器 github.com/angular-ui/ui-router/wiki#resolve 的实例化 -
我用resolve试过了,但是列表页面需要很长时间才能加载。我想显示列表页面,异步加载列表,然后加载下一个状态。
标签: javascript angularjs angular-ui-router singleton angular-promise