我有这个 Controller :
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'
})
在显示其中一项的详细信息之前,我想确保项数组已经初始化。
没有代码复制怎么可能? (如果还有,每个里面都有相同的代码)
谢谢。
最佳答案
你可以使用状态resolve在父级别(这是您问题的确切答案)
或者只使用服务并在那里进行初始化。服务是单例的。
关于javascript - Angular 作用域单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35203923/