angular-ui-router 1.0.x:event.preventDefault&event.defaultPrevented替代 [英] angular-ui-router 1.0.x: event.preventDefault & event.defaultPrevented alternative
问题描述
我只是将 $ stateChangeStart
替换为 $ 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:
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屋!