angular.js ui-route 如何在拒绝后捕获路由或 url 或参数? [英] angular.js ui-route how catch route or url or params after reject?

查看:19
本文介绍了angular.js ui-route 如何在拒绝后捕获路由或 url 或参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获 url 参数或路由或状态被拒绝时:

I want to capture the url params or route or when the state is rejected:

定义状态

app.config(['$stateProvider',
function($stateProvider) {
$stateProvider.state('categories', {
  url: '/categories',
  templateUrl: 'categories/views/index.html',
  resolve: {
   loadRoute: app.loadRoute
  }
 });
}
]);

定义解析事件,默认拒绝

define resolve event , default reject

app.loadRoute = function ($q, $timeout) {
var deferred = $q.defer();
$timeout(deferred.reject);

return deferred.promise;
};

并运行 init catch 错误拒绝

and run for init catch error reject

app.run(['$rootScope', function($rootScope) {
 $rootScope.$on('$stateChangeError',
  function(event, toState, toParams, fromState, fromParams) {       
//.....
  });
}]);

如果我的 url 是例如 /categories?param=1&paramtwo=2 我想要这个 url 验证时继续这个 url

if my url is eg /categories?param=1&paramtwo=2 i want cacth this url for when validate continue this url

如何获取此网址?在事件拒绝时

how cath this url? on event reject

推荐答案

我有几点建议:

  • 首先看一下ui-router 文档用于状态更改事件.
  • 使用观察者的参数获取状态 URL 和参数.
  • 在您的观察者中使用 error 参数来检查不同的错误.
  • 修复对 deferred.reject()
  • 的调用
  • First, take a look at the ui-router documentation for state change events.
  • Get the state URL and params using the arguments of the watcher.
  • Use the error argument in your watcher to check for different errors.
  • Fix your call to deferred.reject()
  • 您不需要使用 $location.
  • 由于您使用的是 ui-router,您可以通过 toState.urltoParams 获取它们.
  • You don't need to use $location.
  • Since you're using ui-router, you can get them with toState.url and toParams.

您可以像这样向 $stateChangeError 事件观察器添加 error 参数:

You can add an error argument to the $stateChangeError event watcher like so:

$rootScope.$on('$stateChangeError', 
function(event, toState, toParams, fromState, fromParams, error){ ... })

正如文档所说,

请务必注意,如果您的 resolve 函数 中有任何错误(javascript 错误、不存在的服务等),它们不会按传统方式抛出.您必须侦听此 $stateChangeError 事件以捕获所有错误.使用 event.preventDefault() 防止 $UrlRouter 将 URL 恢复到以前的有效位置(在 URL 导航的情况下).

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. Use event.preventDefault() to prevent the $UrlRouter from reverting the URL to the previous valid location (in case of a URL navigation).


3.调用 deferred.reject()

  • 更重要的是,您在 $timeout(deferred.reject); 中对 deferred.reject 的调用不是函数调用.立>
  • 应该是 deferred.reject() - (不要忘记括号)

  • 3. Calling deferred.reject()

    • More importantly, your call to deferred.reject in $timeout(deferred.reject); is not a function call.
    • It should be deferred.reject() - (don't forget the parenthesis)
    • 这里是一个例子,它在一秒后拒绝承诺并返回错误 'TEST_ERROR'.观察者记录该错误、预期状态 url 以及触发错误时的参数.

      Here is an example that rejects the promise after one second with the error 'TEST_ERROR'. The watcher logs that error, the intended state url, and it's params when the error is fired.

      解决办法:

        resolve: {
          errorObj: function($q, $timeout) {
            var deferred = $q.defer();
            $timeout(function() {
              deferred.reject("TEST_ERROR");
            }, 1000);
            return deferred.promise;
          }
        }
      

      观察者:

      $rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
        event.preventDefault();
        if (error === "TEST_ERROR") {
          console.log("ERROR:", error, "URL:", toState.url, "PARAMS:", toParams);
        }
      });
      

      这篇关于angular.js ui-route 如何在拒绝后捕获路由或 url 或参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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