$state transition after rejected promise Angular ui-router(拒绝承诺 Angular ui-router 后的 $state 转换)
问题描述
在初始化控制器之前,我正在使用 Angular ui-router 并为我的一种状态设置了解析功能.我检索一些数据,循环并匹配 URL stateParam,如果找到匹配项,则将 promise 解析给控制器并在 promise 中返回该对象.这一切都很好.
I'm using Angular ui-router and have a resolve function set up for one of my states, before the controller is initialized. I retrieve some data, loop through and match it up the URL stateParam, and if a match is found, resolve the promise to the controller and return that object in the promise. That's all working well.
但是,如果找不到匹配项,我只想通过拒绝承诺并运行 $state.go('state');
However, if a match isn't found I simply want to redirect to a different state by rejecting the promise and running $state.go('state');
简单地说:
deferred.reject();
$state.go('state',{params: 'param'});
但这似乎没有任何作用.控制器只是挂起,我没有得到任何控制台错误或任何东西.有什么想法吗?
But this doesn't seem to do anything. The controller just hangs, and I get no console errors or anything. Any ideas?
这个问题适用于 0.xx 版本,很多东西在 1.xx 版本中发生了变化
推荐答案
如果路由解析被拒绝,ui-router 应该抛出 $stateChangeError
.你需要注意这个事件,并在那里触发你的状态转换.
ui-router is supposed to throw a $stateChangeError
if a route resolve is rejected. You need to watch for this event, and trigger your state transition there.
根据维基:
$stateChangeError
- 在转换过程中发生错误时触发.重要的是要注意,如果您的解析函数中有任何错误(javascript 错误、不存在的服务等),它们通常不会抛出.您必须侦听此 $stateChangeError 事件才能捕获所有错误.
$stateChangeError
- fired when an error occurs during transition. It's important to note that if you have any errors in your resolve functions (javascript errors, non-existent services, etc) they will not throw traditionally. You must listen for this $stateChangeError event to catch ALL errors.
https://github.com/angular-ui/ui-router/wiki#wiki-state-change-events
正如评论中提到的@gustavohenke,放置此处理程序的好地方是您应用的主要 .run()
函数.
As @gustavohenke mentioned in the comments, a good place to put this handler is your app's primary .run()
function.
这篇关于拒绝承诺 Angular ui-router 后的 $state 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:拒绝承诺 Angular ui-router 后的 $state 转换


- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01