在处理用户界面的路由器解决功能错误? (又名$ stateChangeError)将数据传递到错误状态? [英] Handling error in ui-routers resolve function? (aka $stateChangeError) Passing data to error state?

查看:182
本文介绍了在处理用户界面的路由器解决功能错误? (又名$ stateChangeError)将数据传递到错误状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我通过处理路由/状态UI路由器。如果一切正常 - 这是伟大的。但究竟什么是的的方式来处理内部发生的错误决心功能?

Inside my Angular application I handle routes/states via ui-router. If everything works - it is great. But what is a good way to handle errors occurring inside resolve functions?

我目前的解决方案:
我有一个到专用错误状态(类似常见的 404.html )。它看起来像这样:

My current solution: I have a to dedicated error state (similar to the common 404.html). Which looks like this:

// inside config()
.state('error', {
  url: '/error',
  controller: 'ErrorCtrl',
  templateUrl: 'error.html' // displays an error message
})

如果在出现错误解析我通过广播 $ stateChangeError 以m 运行功能:

If an error occurs inside resolve I catch it via the broadcasted $stateChangeError in m run function:

angular.module('myModule').run(function($state) {
  $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
    event.preventDefault();
    $state.go('error');
  });
});

本作品,我想改变我的'的error.html 取决于错误里面我的错误消息。我不想污染 $ rootScope ,我想这样做在 UI路由器'埃斯克方式

This works, but I want to change my error message inside my 'error.html' dependent on the error. I don't want to pollute the $rootScope and I want to do it in a ui-router'esk way.

我目前的解决方案是使用 $ stateParams 来的错误数据,我的错误的状态,但我不得不使用JSONified查询参数,这和得到一个非常难看网址:

My current solution uses $stateParams to the error data to my error state, but I have to use JSONified query params for this and get a very ugly URL:

// inside config()
.state('error', {
  url: '/error?data&status&config', // accept these three params
  controller: 'ErrorCtrl',
  templateUrl: 'error.html' // displays an error message
})

angular.module('myModule').run(function($state) {
  $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
    event.preventDefault();
    $state.go('error', JSON.stringify(error)); // error has data, status and config properties
  });
});


是否有不同的方式我怎么能传递错误对象到我的错误状态而不uglifying我的网址是什么? (注:我的错误对象是复杂的,不只是一个简单的字符串)

Question Is there a different way how I can pass the error object to my error state without uglifying my URL? (Note: My error object is complex and not just a simple string.)

推荐答案

您可以通过服务传递数据,你应该在你的错误控制器或错误状态的OnEnter方法​​访问。

You could pass data through a service, that you should access in your error controller or in onEnter method of error state.

或者你也可以丰富的错误状态。也许这不是角的方式,但我的意思是:

Or you could "enrich" your error state. Maybe it's NOT the angular way, but what I mean is:

$rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
  event.preventDefault();
  $state.get('error').error = { code: 123, description: 'Exception stack trace' }
  return $state.go('error');
});

而在你的错误状态的配置:

And in your error state config:

.state('error', {
    url: 'error',
    resolve: {
        errorObj: [function () {
            return this.self.error;
        }]
    },
    controller: 'ErrorCtrl',
    templateUrl: 'error.html' // displays an error message
});

希望这有助于

这篇关于在处理用户界面的路由器解决功能错误? (又名$ stateChangeError)将数据传递到错误状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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