我如何解析网址参数与Angularjs哈希后? [英] How do I parse URL params after a hash with Angularjs?
问题描述
我试图解析来自四方的的access_token其中URL是这样的:
<一个href=\"https://mywebsite.com/4sqredirect/#access_token=1234567890XXXXX\">https://mywebsite.com/4sqredirect/#access_token=1234567890XXXXX
我试过$ routeParams和$位置,并得到什么返回。我试过$航线只有当,我没有得到一个对象返回在它以下属性:
电流:{
PARAMS:{}
pathParams:{}
loadedTemplateUrl:谐音/ 4sqredirect
当地人:{}
范围: {
这个: {
$ REF:$ [当前] [范围]
}
路线:{
$ REF:$
}
位置: { }
令牌:空
}
}
这是否意味着没有办法使用散列的本地AngularJS职能的事业得到它?
更新:
我的控制器看起来像如下:
angular.module('对myApp')
.controller('4sqredirectCtrl',函数($范围,$路径,$位置$ routeParams){
$ scope.route = $路线;
$ scope.location = $位置;
$ scope.token = $ routeParams.access_token;
});
我的主要JS看起来如下:
angular.module('对myApp',[
ngCookies',
ngResource',
ngSanitize',
ngRoute
])
的.config(函数($ routeProvider,$ locationProvider){
$ locationProvider.html5Mode(真); $ routeProvider
。什么时候('/', {
templateUrl:'谐音/主',
控制器:'MainCtrl
})
。当('/ 4sqredirect /',{
templateUrl:'谐音/ 4sqredirect',
控制器:'4sqredirectCtrl
})
。除此以外({
redirectTo:'/'
});
});
位置服务 $的location.hash()
方法返回#后散列
因此,如果您的网址是看起来像 https://mywebsite.com/4sqredirect/#access_token=1234567890XXXXX
然后 $的location.hash()
收益的access_token = 1234567890XXXXX
你需要把它分解拆分('=')[1]
看到这个 plunker 当你点击 4Square
然后 $ location.url()
收益 / 4sqredirect /#=的access_token 123456
和 $的location.hash()。斯普利特('=')[1]
收益 123456
I'm trying to parse for the access_token from Foursquare where the URL is like this:
https://mywebsite.com/4sqredirect/#access_token=1234567890XXXXX
I've tried $routeParams and $location and get nothing returned. Only after I tried $route, I did get an object back with the following attribute in it:
current: {
params: { }
pathParams: { }
loadedTemplateUrl: partials/4sqredirect
locals: { }
scope: {
this: {
$ref: $["current"]["scope"]
}
route: {
$ref: $
}
location: { }
token: null
}
}
Does this mean there's no way to get it using native AngularJS functions cause of the hash?
UPDATE:
my controller looks like as follows:
angular.module('myApp')
.controller('4sqredirectCtrl', function ($scope, $route, $location, $routeParams) {
$scope.route = $route;
$scope.location = $location;
$scope.token = $routeParams.access_token;
});
my main js looks like as follows:
angular.module('myApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute'
])
.config(function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/', {
templateUrl: 'partials/main',
controller: 'MainCtrl'
})
.when('/4sqredirect/', {
templateUrl: 'partials/4sqredirect',
controller: '4sqredirectCtrl'
})
.otherwise({
redirectTo: '/'
});
});
From angular location service $location.hash()
method return #after-hash
so if your url is look like https://mywebsite.com/4sqredirect/#access_token=1234567890XXXXX
then $location.hash()
return access_token=1234567890XXXXX
you need to split it split('=')[1]
see this plunker when you click 4Square
then $location.url()
return /4sqredirect/#access_token=123456
and $location.hash().split('=')[1]
return 123456
这篇关于我如何解析网址参数与Angularjs哈希后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!