本文介绍了angular.js UI的路线追赶路径或URL或PARAMS如何拒绝后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要捕获的网址参数或路由或当状态被拒绝:
定义状态
的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){
// .....
});
}]);
如果我的网址是如的 /类别参数= 1&安培; paramtwo = 2 的我想cacth这个网址验证的时候继续这一网址
解决方案
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
andtoParams
.
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,
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);
}
});
这篇关于angular.js UI的路线追赶路径或URL或PARAMS如何拒绝后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!