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

前端 未结 6 1161
逝去的感伤
逝去的感伤 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:26

    I know this is not Angular but its pure JS and works like a charm (just don't add dummyPath and it will take the URL).

    function getUrlParameter(param, dummyPath) {
            var sPageURL = dummyPath || window.location.search.substring(1),
                sURLVariables = sPageURL.split(/[&||?]/),
                res;
    
            for (var i = 0; i < sURLVariables.length; i += 1) {
                var paramName = sURLVariables[i],
                    sParameterName = (paramName || '').split('=');
    
                if (sParameterName[0] === param) {
                    res = sParameterName[1];
                }
            }
    
            return res;
    }
    

    Usage:

    var sportdsId = getUrlParameter('sportsId');
    
    0 讨论(0)
  • 2020-12-29 09:27

    Create your self a search object from the the $window.location.search property using the following:

        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;
    

    or create a queryString factory which you can use anywhere.. This combines the left and right parts of the queryString.. I.e. before # and after the #

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

    usage:

    var qs = queryString.search();
    var sportsId = sq.sportsId;
    
    0 讨论(0)
  • 2020-12-29 09:28

    Three simple steps did the Job:

    1. Config angular for HTML5 mode by adding the following code to your controller/service:

      app.config(['$locationProvider', function ($locationProvider) { $locationProvider.html5Mode(true); }]);

    2. Add base element to your HTML:

    <base href="/">

    1. Inject "$location" into your angular controller:

      app.controller('ABCController', ['$scope', '$location', ABCController]);

    Finally to get the query string use the following code:

    var yourId = $location.search().yourId;
    

    The answer is already there in previous answers, I just summarized for better understanding. Hopefully somebody will become beneficial from it.

    0 讨论(0)
  • 2020-12-29 09:42

    Please refer below links

    var goto = $location.search().sportsId;
    

    Getting values from query string in an url using AngularJS $location

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-29 09:50

    You need to use

    $location.search()
    

    This returns an object with your params so lets say you have url?param1=val&param2=val2

    This will give the following back

    { param1 : val, param2 : val2 }
    

    Also make sure you enable

    $locationProvider.html5Mode(true);  
    
    0 讨论(0)
提交回复
热议问题