在 AngularJS 中解析请求的 URL 的最佳方法

2023-12-27

我有一个像这样的网址:

http://www.something.com/project/edit/987654321

解析的最佳方法是什么987654321使用 AngularJS 的 URL 的一部分? Angular 中有辅助函数吗? (我不想使用 jQuery)

如果我理解正确的话,AngularJS 中的路由功能将无法工作,除非您在 URL 中使用 # 或启用了 HTML5 模式。

我们需要在页面首次加载时解析这个 URL。


我将根据标题回答这个问题,因为我来到这里也是为了寻找一种解析网址的方法。这是我在小提琴手页面上找到了。

var parser = document.createElement('a');
parser.href = "http://www.example.com";
//At this point it's parsed and all the info is on the parser object
console.log(parser.protocol);
console.log(parser.hash);
console.log(parser.hostname);
console.log(parser.port);
console.log(parser.pathname);
console.log(parser.search);

[Update]
我刚刚注意到有一个 URL 类。

var parsedURL = new URL('http://www.example.com');
console.log(parsedURL.hash);
console.log(parsedURL.protocol);
console.log(parsedURL.host);
//etc
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 AngularJS 中解析请求的 URL 的最佳方法 的相关文章

随机推荐