Javascript AngularJS:获取链接中没有散列的查询字符串值
我正在尝试使用angularjs获取查询字符串值 我的Url:Javascript AngularJS:获取链接中没有散列的查询字符串值,javascript,php,angularjs,Javascript,Php,Angularjs,我正在尝试使用angularjs获取查询字符串值 我的Url:http://localhost/example.php?sportsId=3 当我应用var goto=$location.search()['sportsId']时 它返回未定义的我 但是,如果我在url中添加哈希,如url:http://localhost/example.php#?sportsId=3 然后它返回正确的值3 但在本例中,它也给了我错误:[$parse:syntax]http://errors.angularjs
http://localhost/example.php?sportsId=3
当我应用var goto=$location.search()['sportsId']时代码>
它返回未定义的我
但是,如果我在url中添加哈希,如url:http://localhost/example.php#?sportsId=3
然后它返回正确的值3
但在本例中,它也给了我错误:[$parse:syntax]http://errors.angularjs.org/1.3.8/$parse/syntax?p0=undefined&p1=not%20a%20primary%20expression&p2=null&p3=sportsID%3D&p4=sportsID%3D
另外,我的默认$\u请求['sportsId']
不能使用哈希格式
如何使用angularjs从查询字符串中正确获取值?我知道这不是angularjs,而是纯JS,工作起来很有魅力(只是不要添加dummyPath,它会占用URL)
请参考以下链接
var goto = $location.search().sportsId;
您需要使用
$location.search()
这将返回一个带有参数的对象,因此假设您有
url?param1=val¶m2=val2
这将返回以下内容
{ param1 : val, param2 : val2 }
还要确保启用
$locationProvider.html5Mode(true);
从$window.location.search
属性中创建self-a搜索对象,方法如下:
var search = $window.location.search
.split(/[&||?]/)
.filter(function (x) { return x.indexOf("=") > -1; })
.map(function (x) { return x.split(/=/); })
.map(function (x) {
x[1] = x[1].replace(/\+/g, " ");
return x;
})
.reduce(function (acc, current) {
acc[current[0]] = current[1];
return acc;
}, {});
var sportsId = search.sportsId;
或者创建一个queryString
工厂,您可以在任何地方使用它。。这将组合查询字符串的左侧和右侧部分。。即
之前和
之后
用法:
var qs = queryString.search();
var sportsId = sq.sportsId;
三个简单的步骤完成了这项工作:
通过向控制器/服务中添加以下代码来配置HTML5模式:
app.config(['$locationProvider',函数($locationProvider){
$locationProvider.html5Mode(true);
}]);
将基本元素添加到HTML中:
这就是我使用的。它本质上与接受的答案相同,但将所有参数作为对象返回,并处理url组件的解码
function getUrlParameters() {
var pairs = window.location.search.substring(1).split(/[&?]/);
var res = {}, i, pair;
for (i = 0; i < pairs.length; i++) {
pair = pairs[i].split('=');
if (pair[1])
res[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return res;
}
函数getUrlParameters(){
var pairs=window.location.search.substring(1.split(/[&?]/);
var res={},i,pair;
对于(i=0;i
我认为以下问题可以帮助您解决问题。[[1]:var goto=$location.search().sportsId;不起作用。返回我未定义此答案要求您调整$locationProvider,这可能在所有实例中都不起作用。很好。只需解码字符串:res=decodeURIComponent(sParameterName[1]。替换(/\+/g,”);我不知道,但对于一个简单的操作来说,它看起来代码太多了。
(function () {
"use strict";
angular
.module("myModuleName")
.factory("queryString", QueryStringFactory);
function QueryStringFactory($window, $location) {
function search() {
var left = $window.location.search
.split(/[&||?]/)
.filter(function (x) { return x.indexOf("=") > -1; })
.map(function (x) { return x.split(/=/); })
.map(function (x) {
x[1] = x[1].replace(/\+/g, " ");
return x;
})
.reduce(function (acc, current) {
acc[current[0]] = current[1];
return acc;
}, {});
var right = $location.search() || {};
var leftAndRight = Object.keys(right)
.reduce(function(acc, current) {
acc[current] = right[current];
return acc;
}, left);
return leftAndRight;
}
return {
search: search
};
}
}());
var qs = queryString.search();
var sportsId = sq.sportsId;
function getUrlParameters() {
var pairs = window.location.search.substring(1).split(/[&?]/);
var res = {}, i, pair;
for (i = 0; i < pairs.length; i++) {
pair = pairs[i].split('=');
if (pair[1])
res[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return res;
}