如何将更有意义的错误信息传递给$ routeChangeError? [英] How do you pass more meaningful error information to $routeChangeError?

查看:85
本文介绍了如何将更有意义的错误信息传递给$ routeChangeError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有任何类型的错误,我的服务器将其传递给浏览器时,我将获得非常有意义且功能强大的错误消息.但是我如何在下面访问该信息:

I have incredibly meaningful and powerful error messages that my server passes to the browser if there was any kind of error. But how do I access that info in the following:

$rootScope.$on("$routeChangeError", function (event, current, previous, rejection) {
    console.log(rejection); // "An unknown error has occurred."
});

我在路由定义中使用了$ routeProvides.resolve,因此,如果这些承诺没有解决,则$ routeChangeError将成为我的处理方式.我是否可以通过某种方式访问​​服务器的响应并以某种方式显示该响应?

I'm using $routeProvides.resolve in my route definitions, and so $routeChangeError was going to be my way of handling if those promises didn't resolve. Is there for a way for me to access the response from the server and display that somehow?

谢谢!

推荐答案

resolve对象的每个属性都应该是一个返回promise的函数.因此,如果您的一条路线不能解决,在.catch处理程序中抛出原因会将错误信息传递给$ routeChangeError处理程序

Each property on a resolve object should be a function that returns a promise. So if one of your routes doesn't resolve, throwing a reason in the .catch handler will pass your error information to the $routeChangeError handler

$routeProvider
  .when("/foo", {
     templateUrl: "foo.html",
     controller: "fooController",
     resolve: {
         message: function($http){
             return $http.get('/someUrl').
             then(function(response) {
               return response.data;
             }).
             catch(function(response) { 
               throw response.data;
             });
     }
  }
});

假设data参数包含您要使用的服务器中的数据,它将最终出现在$ routeChangeError事件的拒绝参数中.

Assuming the data parameter has the data from the server you want to use, this will end up in the rejection parameter on the $routeChangeError event.

这篇关于如何将更有意义的错误信息传递给$ routeChangeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆