angular.js UI的路线追赶路径或URL或PARAMS如何拒绝后?路径、路线、js、angular

2023-09-13 05:00:33 作者:ら情若相依°

我要捕获的网址参数或路由或当状态被拒绝:

定义状态

 的app.config(['$ stateProvider',功能($ stateProvider){$ stateProvider.state('类',{  网址:'/类别,  templateUrl:类别/视图/ index.html的,  解析:{   loadRoute:app.loadRoute  } });}]); 

定义解决事件,默认拒绝

  app.loadRoute =功能($ Q $超时){变种推迟= $ q.defer();$超时(deferred.reject);返回deferred.promise;}; 

和运行初始化catch错误拒绝

  app.run(['$ rootScope',函数($ rootScope){ $ rootScope。在$('$ stateChangeError',  功能(事件,toState,toParams,fromState,fromParams){// .....  });}]); 
AngularJS 下一个大框架

如果我的网址是如的 /类别参数= 1&安培; paramtwo = 2 的我想cacth这个网址验证的时候继续这一网址

  

如何导管这个网址?的事件拒绝

解决方案

我有几个建议:

首先,来看看为 UI的路由器文档状态更改事件的。获取国家URL,并使用观察者的参数PARAMS。使用错误参数在守望检查不同的错误。修正你对 deferred.reject调用()

1。获取URL和参数

您不需要使用 $位置。 由于您使用的用户界面的路由器,你可以用 toState.url 让他们和 toParams

2。使用错误参数在 $ stateChangeError

您可以添加错误参数到$ stateChangeError事件观察,像这样:

  $ rootScope。在$('$ stateChangeError',功能(事件,toState,toParams,fromState,fromParams,错误){...}) 

由于文件说,

  

要注意,如果你在你的解析功能任何错误(JavaScript错误的,不存在服务等),他们将传统上不扔这一点很重要。你必须听此$ stateChangeError事件捕捉所有的错误。使用事件。preventDefault()来prevent的$ UrlRouter从恢复的URL previous有效的位置(以URL导航的情况下)

3。调用 deferred.reject()

更重要的是后,您的来电 deferred.reject $超时(deferred.reject); 不是一个函数调用。 这应该是 deferred.reject() - (别忘了括号)

4。示例

下面是与误差'TEST_ERROR 后一秒拒绝承诺的例子。观察者记录的错误,预期状态的URL,这是当错误被开除PARAMS。

的决心:

解析:{    errorObj:函数($ Q $超时){      变种推迟= $ q.defer();      $超时(函数(){        deferred.reject(TEST_ERROR);      },1000);      返回deferred.promise;    }  }

的守望者:

  $ rootScope。在$($ stateChangeError功能(事件,toState,toParams,fromState,fromParams,错误){  。事件preventDefault();  如果(错误===TEST_ERROR){    的console.log(错误:错误,网址:toState.url,PARAMS:toParams);  }}); 

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

define state

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;
};

and run for init catch error reject

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

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

解决方案

I have a few suggestions:

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()

1. Getting the URL and parameters

You don't need to use $location. Since you're using ui-router, you can get them with toState.url and toParams.

2. Using the error argument in $stateChangeError

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

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

As the documentation says,

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. 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)

4. Example

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.

The resolve:

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

The watcher:

$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);
  }
});

 
精彩推荐
图片推荐