简体   繁体   中英

Is there a way to determine in angular.js if there are url query parameters present?

In angular routes, you can parse the url arguments into $routeParams. However, I need to know what's the best way to see if they exist.

For example, I have several $routeParams from different parts of the URL however, all I want to know is whether any url params exist (if the ? exists and parameters after). I understand, I can just do a search for the ? but wanted to know if there was a way within angular.

Here's my code:

 $routeProvider.when('/sports/:sport_id/:args?', 

sport_id and all the args will be combined in $routeParams. However I just want to know if args exist. (the query parameters)

You can inject $routeParams and look for the presence of any properties in the object.

ex: var hasParams = Boolean(Object.keys($routeParams).length);

You dont need the Boolean( part as 0 will be falsy anyways, however just being more explicit.

If looking for just the query string params you can just inject $location service and use $location.search() it will provide object with query string argument as key value pair on the object.

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