3

I'm currently working on a large refactoring project where my team is replacing old functionality in an application with more modern approach.

We are moving towards a single page application approach, but before we get there, we are replacing single independent web pages with AngularJS functionality.

Our backend is ASP.NET MVC application and I'm forced to use whatever I currently have; thus the URL's and such need to stay in given format:

http://example.com/controller/action/id

How can I read URL parts in my AngularJS controller, without using the so called hash-style URL's that AngularJS single page apps mostly use?

For example; I go to /user/edit/1 and need to get that id of '1' in my AngularJS controller somehow.

Any help would be greatly appreciated!

1
  • I don't think it is a good approach to add AngularJS to every single page as different applications. You can implement a single page application with refactored pages and a router that will redirect to old pages when they are yet to be refactored. Commented Oct 16, 2014 at 10:06

2 Answers 2

1

The $location service can help you here. As per documentation https://code.angularjs.org/1.2.15/docs/guide/$location

$location service provides getter methods for read-only parts of the URL (absUrl, protocol, host, port) and getter / setter methods for url, path, search, hash:

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer! Looked into it and looks like it's something I can use, but are you aware of any other ways to accomplish this? Preferred way would be to automatically define the format of the URL and parse the id values from there for the controller to use. Something like you can do in the ngRoute module, but without defining controllers, templates and such and only passing the URL parts to the controller.
I don't know if there is something available readily, but you can always create a service of your own to parse the url and provide data in a consistent manner.
Thanks. I wrote a service like you suggested and added a possibility to define routes just like in ngRoute module. Works well with $location. Thank you for pointing me to a right direction!
Great it helped, but remember that you service will work as long the server pattern for url follow a specific convention.
0

Define your route like this

angular.module('app', [])
.config(['$routeProvider', function($routeProvider) {

  $routeProvider.when('/user/edit/:id', {
    templateUrl: '/edit.html',
    controller: 'editCtrl'
  })
}]);

// inside your controller you do this

angular.module('app').controller('editCtrl', ['$scope', '$routeParams', function($scope, $routeParams){
   // $routeParams is the key here
   // you can access it by doing

   console.log($routeParams.id)
}])

1 Comment

Thanks for your answer. The problem is, that I don't have templates in use, since it's not a single page application. Also I'm defining the used controller in the HTML document and thus can't really use the ngRoute module. Is it possible to use ngRoute without defining controller and template and only pass route values to the controller?

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.