$state transition after rejected promise Angular ui-router

筅森魡賤 提交于 2019-11-28 08:20:11
Matt Way

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.

You're misunderstanding how promises work. deferred.reject() simply notifies the deferred.promise that it has been rejected.

var promise = deferred.promise;

promise.then(
  function (result) { 
    // this function runs if the promise was resolved
  },
  function (result) {
    // this function runs if the promise was rejected
    // this is where you should put $state.go(..)
  }
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!