简体   繁体   English

Angular JS - 检索 URL 参数

[英]Angular JS - Retrieve URL parameters

What is the correct Angular approach to retrieving the URL parameters?检索 URL 参数的正确 Angular 方法是什么?

Example: http://example.com/mypage.html?product=1234&region=4&lang=en示例: http : //example.com/mypage.html?product=1234&region=4&lang=en

Thanks谢谢

This will convert your query into an object这会将您的查询转换为对象

var queryData = url.split('?')[url.split('?').length - 1].split('&').reduce(function(prev, curr){
    var fieldName = curr.split('=')[0]; 
    var value = curr.split('=').length > 1 ? curr.split('=')[1] : '';
    prev[fieldName] = value; 
    return prev
}, {});

And then you can access them by queryData.product, for example.然后你可以通过 queryData.product 访问它们,例如。 Not angular, but it's a solution.不是角度,但它是一个解决方案。

For the angular way, you can use $location.search() as described here http://www.angulartutorial.net/2015/04/get-url-parameter-using-angular-js.html对于角度方式,您可以使用 $location.search() 如此处所述http://www.angulartutorial.net/2015/04/get-url-parameter-using-angular-js.html

You can use the $location service.您可以使用 $location 服务。 For example:例如:

 angular.module('parameters', []).run(['$location', function($location) { var params = $location.search(); } ]);

I posted the same question with more details, and found a perfect response.我发布了更多详细信息的相同问题,并找到了完美的答案。 Please see DavidL's response in my question here:请在此处查看 DavidL 在我的问题中的回答:

Reading URL parameters in AngularJS - A simple way? 在 AngularJS 中读取 URL 参数 - 一个简单的方法?

Thanks to everyone who assisted me in this thread;感谢在此线程中帮助我的所有人; perhaps I didn't include enough details in the original question.也许我没有在原始问题中包含足够的细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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