2017-01-17 4 views
1

Как отменить переход в onBefore? Этот фрагмент, вероятно, может показать свое намерение:UI-маршрутизатор | приостановить переход | onBefore

$transitions.onBefore({}, (trans) => { 
    console.log("before a transition"); 
    console.log(trans); 

    //I want to broadcast an event here 
    //notifying inner components that it's about to switch state 
    //The event will contain the trans 
    //So I actually don't need to wait here, I just need to suspend the trans at this point. 

    //The inner component will respond by emitting and event. 
    //That event will contain approval status, and the trans object. 
    //My code can simply check the approval status; If false, 
    //it simply does nothing. Otherwise it will pick and resume the trans. 
}); 

Благодарность

ответ

0

Фигурной его. Это показывает идею.

Во-первых, имеют window.counter значение 0 в ваших boot.js/app.js ..., а затем в контроллер в $ OnInit:

$transitions.onBefore({}, (trans) => { 
    if (window.counter == 0) { 
    this.$timeout(() => trans.run(), 2000); 
    window.counter += 1; 
    return false; 
    } 
}); 

Вы заметите в первый ваш переход отменяется ..., и через 2 секунды он снова запускается.

0

A TransitionHookFn возвращает HookResult. Это может быть в форме обещания отложить переход до тех пор, пока не будет разрешено обещание.

В этом случае, мы можем сделать следующее:

$transitions.onBefore({}, $transitions => { 
    return this.$timeout(() => { 
    /* 
    * Do something here. 
    * Once this is completed, transition can resume 
    * unless promise is rejected or resolves to a false value 
    */ 
    }, 2000); 
}); 

Ссылка: https://ui-router.github.io/ng1/docs/latest/modules/transition.html#hookresult

Смежные вопросы