我使用Angular UI-router模块导航到嵌套状态。
我有一个名为program
的状态,以及一个名为program.dashboard
的状态。
每当用户路由到program
(不提供嵌套路由)时,它应该重定向到program.dashboard
。如何从州内了解它是否已经通过嵌套路线?
我遇到的问题是,路由到program
下的任何嵌套路由导致重定向到program.dashboard
,因为在评估程序路由时,它对子进程一无所知还有待装载的路线。
这里的答案类似于答案详细说明了我遇到的问题。他们指出,您永远无法导航到除program
之外的program.dashboard
以下的任何州,这正是我想要检查它的位置和条件的原因重定向。 Angular JS ui-router how to redirect to a child state from a parent?
任何帮助都将不胜感激。
.state('program', {
url: "/program/{programId:[0-9]{1,8}}",
templateUrl: "components-child",
controller: function($scope, $rootScope, orgService, $state,
$stateParams) {
var vm = this;
vm.curentOrganization = null;
vm.currentProgram = null;
$scope.$watch(function() {
return orgService.currentProgram();
},
function(newVal, oldVal) {
if (newVal != null) {
//this causes a redirect to program.dashboard
//even if I want to go to..say.. program.settings
$state.go('program.dashboard',
{ programId: newVal.programId });
vm.currentProgram = newVal;
}
}, true);
},
params: { programId: { value: '{{currentProgram.programId}}' } },
})
.state('program.dashboard', {
url: "/dashboard",
templateUrl: "dashboard-dashboard",
controller: 'DashboardController as vm',
})
答案 0 :(得分:0)
如果我理解你的意图,你可能想听$ stateChangeStart,而不是看服务价值。
app.run(function($state, $rootScope) {
$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {
if (toState.name === 'program') {
// this is a transition directly to 'program'
event.preventDefault(); // cancel the transition
$state.go('program.dashboard', toParams); // push them to 'program.dashboard'
}
});
});