使用Angular js从url文本中获取参数

时间:2017-07-18 23:10:16

标签: angularjs

我从后端获取此网址,并且只需要从这里获取一个参数(游戏编号)

https://www.example.com/#/?category=all&provider=all&game=3189&type=fun

如何在控制器中获得游戏编号(3189)?

修改

它不是$ location

1 个答案:

答案 0 :(得分:2)

从网站获取

function GetParameter(parameterName) {
    var result = null;
    var tmp = [];
    var items = location.href.substr(1).split("&");
    for (var index = 0; index < items.length; index++) {
        tmp = items[index].split("=");
        if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
    }
    return result;
}

从STRING中获取

function GetParameterFromString(parameterName, url) {
    var result = null;
    var tmp = [];
    var items = url.split("&");
    for (var index = 0; index < items.length; index++) {
        tmp = items[index].split("=");
        if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
    }
    return result;
}
然后你会打电话给

var url = "example.com/#/?category=all&provider=all&game=3189&type=fun‌​";
var gameNumber = GetParameterFromString("game" url);