简体   繁体   中英

Search URL parameters with Angular JS

Say I have a URL like this:

www.mysite.com?account=23&token=asdu756sdg65sdf

Now, I need to access those URL parameters individually using Angular JS .

I've tried using location.search inside my controller:

console.log(location.search);

Which nicely returns the full query string:

?account=23&token=asdu756sdg65sdf

But it seems to return a string, not an object. Is there an easy way to access the individual ids and values in the string?

I've also tried:

console.log($location.search());

Which seems to return an empty object.

Try with

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

but the url should be like /#!/?account=1

您是否尝试过将其用作功能?

$location.search();

pure js way:

var getParamFromURL = function(_url) {
    var url = _url;
    var _paraString = url.indexOf("&") > -1 ? url.substring(url.indexOf("?") + 1, url.length).split("&") : url.substring(url.indexOf("?") + 1, url.length).split('');
    var _paraObj = {};
    for (i = 0; p = _paraString[i]; i++) {
        _paraObj[p.substring(0, p.indexOf("=")).toLowerCase()] = p.substring(p.indexOf("=") + 1, p.length);
    }
    return _paraObj;
}

via angular js:

 // Given:
 // URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
 // Route: /Chapter/:chapterId/Section/:sectionId
 //
 // Then
 $routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}

Great. @Cooper is right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM