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

Javascript 在AngularJS中解析请求URL的最佳方法,javascript,angularjs,Javascript,Angularjs,我有一个类似的URL: http://www.something.com/project/edit/987654321 使用AngularJS解析URL的987654321部分的最佳方法是什么?在Angular中是否有任何辅助函数?(我不想使用jQuery) 如果我理解正确,AngularJS中的路由功能将无法工作,除非您在URL中使用#或启用HTML5模式 我们需要在首次加载页面时解析此URL。您可以使用$location\path()函数: # given: url = http://www

我有一个类似的URL:

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

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

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

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

您可以使用
$location\path()
函数:

# given: url = http://www.something.com/project/edit/987654321
$location.path().split('/').pop();
然而,听起来您好像遇到了路由问题。查看,其中显示了如何在
app.js
配置中正确使用
$routeProvider
路由。在本教程中:

phonecatApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
       when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      })
      .otherwise( ...)  //omitting remainder of cut/paste
因此,您的
app.js
将具有如下路由定义:

ender2050App.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/project/edit/:id', {
        templateUrl: 'partials/editor.html',
        controller: 'EditorCtrl'
      })
并且您的控制器文件(即controllers.js)将具有
EditorCtrl
,它注入
$routeParams
服务以访问
id
变量


如果您需要更定制的解析选项,请查看。

问题和答案都不尊重标题。我登陆这里是为了搜索如何解析angular中的任何url。这个问题是关于解析请求的url的。如果我想解析
http://www.google.com


编辑它。

我将根据标题回答这个问题,因为我来到这里也是为了寻找解析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);
[更新]
我刚刚注意到有一个URL类

var parsedURL = new URL('http://www.example.com');
console.log(parsedURL.hash);
console.log(parsedURL.protocol);
console.log(parsedURL.host);
//etc

解析是什么意思?使用document.URL并用regex或substring/indexofindestigate解析$location servicefood point,但最好是作为注释,而不是作为答案发布。如果您查看我的答案中的更新,您将看到如何解析任何URL。即使答案的第一部分也可以。谢谢,我从未使用过URL类。IE中根本不支持URL