angular-ui-router 1.0.x: event.preventDefault &event.defaultPrevented 替代方案 [英] angular-ui-router 1.0.x: event.preventDefault & event.defaultPrevented alternative
问题描述
我刚刚用 $transitions.onStart
$rootScope.$on('$stateChangeStart', function(e, ...){
e.preventDefault();
// other code goes here...
});
到
$transitions.onStart({}, function(tras){
// need a code equivalent to e.preventDefault
// need a code to identify event.defaultPrevented
// other code goes here...
// get parent states
_o.util.getAncestorStates(toState.name, true).reverse()
.forEach(function (state) {
// certain condition to call event.preventDefault()
if(event.defaultPrevented) {....}
});
});
我想,我们可以通过添加 return $q.reject()
而不是 e.preventDefault()
来防止转换,但我不明白代码是如何下面的 return $q.reject()
会执行.
and I guess, we can prevent the transition by adding return $q.reject()
instead of e.preventDefault()
but I could not understand how the code below return $q.reject()
would execute.
另外,如何替换 event.defaultPrevented
?
我认为应该对 transition.promise
做一些事情,但不清楚.
I think should be done something on the transition.promise
but not clear.
对不起,我看不懂官方文档 - https://ui-router.github.io/ng1/docs/latest/ 很容易.谁能帮我找出更好的解释或替代上述代码?
I am sorry, I can't understand the official doc - https://ui-router.github.io/ng1/docs/latest/ easily. Can anybody help me finding out a better explanation or the replacement for the above code?
推荐答案
您可以根据自己的逻辑选择以下两个选项之一:
You can choose one of these two options depend on your logic:
从 angular-ui-router 1.0.3 开始,您可以使用
$transition.abort()
.如果您必须在异步调用后中止状态更改,则选择更简洁.
Since angular-ui-router 1.0.3 you can use
$transition.abort()
. Cleaner choice if you have to abort state change after an async call.
$transitions.onStart({}, function($transition) {
$transition.abort();
//more code...
});
此外,正如@tanmay 在评论中所说,您可以使用简单的 return false
取消它.这也适用于非稳定版本(1.0.0.beta 和 1.0.0.rc).(在 ui-rooter 文档中查看)
Also, as @tanmay says in comments, you can use a simple return false
to cancel it. This will work in non stable versions too (1.0.0.beta and 1.0.0.rc). (Check it in ui-rooter docs)
$transitions.onStart({}, function($transition) {
//code ...
return false;
});
这篇关于angular-ui-router 1.0.x: event.preventDefault &event.defaultPrevented 替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!