AngularJS: Get query string values without a hash in the link

前端 未结 6 1162
逝去的感伤
逝去的感伤 2020-12-29 09:12

I am trying to get query string values using angularjs.

my Url: http://localhost/example.php?sportsId=3

when I applied var goto = $locati

6条回答
  •  天命终不由人
    2020-12-29 09:49

    This is what I use. It's essentially the same as the accepted answer but returns all parameters as an object and handle decoding of url components.

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

提交回复
热议问题