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

后端 未结 9 967
鱼传尺愫
鱼传尺愫 2020-11-27 04:36

Regarding $location.search, the docs say,

Return search part (as object) of current url when called without any parameter.

In my

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 05:13

    $location.search() returns an object, consisting of the keys as variables and the values as its value. So: if you write your query string like this:

    ?user=test_user_bLzgB
    

    You could easily get the text like so:

    $location.search().user
    

    If you wish not to use a key, value like ?foo=bar, I suggest using a hash #test_user_bLzgB ,

    and calling

    $location.hash()
    

    would return 'test_user_bLzgB' which is the data you wish to retrieve.

    Additional info:

    If you used the query string method and you are getting an empty object with $location.search(), it is probably because Angular is using the hashbang strategy instead of the html5 one... To get it working, add this config to your module

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

提交回复
热议问题