2017-01-17 1 views
1

Wie kann ich einen Übergang in einem onBefore abbrechen? Dieser Code-Schnipsel kann wahrscheinlich meine Absicht verdeutlichen:UI-Router | suspendiere einen Übergang | 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. 
}); 

Dank

Antwort

0

figured it. Dies zeigt die Idee.

Erstens haben window.counter auf 0 in Ihrem boot.js gesetzt/app.js ... und dann in Ihrer Steuerung $ onInit:

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

Sie zunächst feststellen, wird Ihr Übergang abgebrochen ... und nach 2 Sekunden wird es erneut ausgeführt.

0

Eine TransitionHookFn gibt eine HookResult zurück. Dies kann die Form eines Versprechens haben, den Übergang zu verschieben, bis das Versprechen gelöst ist.

In diesem Fall können wir folgendes tun:

$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); 
}); 

Referenz: https://ui-router.github.io/ng1/docs/latest/modules/transition.html#hookresult

Verwandte Themen