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()
,但是我不明白下面的代码如何返回$ 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在评论中所说,您可以使用简单的返回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屋!