5

I have a URL like:

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

What's the best way to parse the 987654321 part of the URL using AngularJS? Are there any helper functions within Angular? (I'd prefer to not use jQuery)

If I understand it correctly, the routing functions within AngularJS won't work unless you use a # within the URL or enabled HTML5 mode.

We need to parse this URL when the page is first loaded.

3
  • what do you mean by parse? Commented Feb 20, 2014 at 21:41
  • 1
    Use document.URL and parse with regex or substring/indexOf Commented Feb 20, 2014 at 21:50
  • 1
    Investigate the $location service Commented Feb 20, 2014 at 22:08

3 Answers 3

11

I'll answer the question based on the title because I landed here looking for a way to parse a url as well. Here's the answer I found on a fiddler page.

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]
I just noticed there's a URL class.

var parsedURL = new URL('http://www.example.com');
console.log(parsedURL.hash);
console.log(parsedURL.protocol);
console.log(parsedURL.host);
//etc
Sign up to request clarification or add additional context in comments.

1 Comment

URL is not supported at all in IE developer.mozilla.org/en-US/docs/Web/API/…
4

You can use the $location service's $location#path() function:

# given: url = http://www.something.com/project/edit/987654321
$location.path().split('/').pop();

However, it sounds like you have a routing issue. Check out the Angular Tutorial on Routing, which shows how to correctly use $routeProvider routes in your app.js configuration. From the tutorial:

phonecatApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
       when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      })
      .otherwise( ...)  //omitting remainder of cut/paste

So your app.js would have a route definition like:

ender2050App.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/project/edit/:id', {
        templateUrl: 'partials/editor.html',
        controller: 'EditorCtrl'
      })

And your controller file (i.e., controllers.js) would have the EditorCtrl that injects the $routeParams service to access the id variable.

If you need a more customized parsing option, check out the $parse service.

Comments

2

Both the question AND the answer don't respect the title. I landed here trying to search how to parse ANY url in angular. This question is about parsing the requested url. What if I want to parse http://www.google.com ??

Edited it.

2 Comments

Good point but this would be better as a comment, instead of being posted as an answer.
If you look at the Update in my answer you'll see how to parse any URL. Even the first part of the answer would do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.