Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

javascript - $state transition after rejected promise Angular ui-router

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.

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');

Simply this:

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?

This question apply to verion 0.xx, many things have changed in version 1.xx

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

As per the wiki:

$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


As @gustavohenke mentioned in the comments, a good place to put this handler is your app's primary .run() function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...