Reputation: 133
How can I register ui-router transition hook using async function in the body? I need redirect to substate based on async result. This is my code, which does not works, because callback does not returns any value:
router.transitionService.onBefore({to: 'app'}, transition => {
let dashboardService: DashboardService = transition.injector().get(DashboardService);
dashboardService.getList().subscribe((list: Array<Dashboard>) => {
if (list.length) {
return transition.router.stateService.target('app.dashboard', {id: list[0].id});
}
});
});
The callback expects a return value to be TransitionHookFn. I tried return Promise and Subject but this caused an error.
Upvotes: 1
Views: 834
Reputation: 133
I found the answer myself. Observable must be transformed into promise:
router.transitionService.onBefore({to: 'app'}, transition => {
let dashboardService: DashboardService = transition.injector().get(DashboardService);
return dashboardService.getList().toPromise().then((list: Array<Dashboard>) => {
if (list.length) {
return transition.router.stateService.target('app.dashboard', {id: list[0].id});
}
})
});
Upvotes: 1