Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Using names/aliases for $routeProvider #1791

@matsko

Description

@matsko

Hey guys.

Keeping track of URLs and hash prefixes manually is pretty tricky and can lead to duplicate URL strings and more testing code. It would be nice if you can name your routes (optionally) and then query those names later on in your application via the $routeProvider service.

So lets say I have a route that goes to the home page:

$routeProvider.when('/home',{
   controller : '...',
   templateUrl : '...'
});

And my HTML links like so:

<a href="#/home">...</a>
or if html5mode is on
<a href="/home">...</a>

This works fine, but if I change the route to from /home to just / then I need to update that.

What I am proposing is that I could patch AngularJS to include support for naming and aliasing routes so that they can be accessed directly in the scope.

$routeProvider.when('/home',{
   name : 'home_path',
   controller : '...',
   templateUrl : '...'
});
$routeProvider.when('/profile/:id/:value',{
   name : 'profile_path',
   controller : '...',
   templateUrl : '...'
});
$routeProvider.alias('root_path', 'home_path'); //now the root_path URL is the same

Then by using either a service or factory you can have access to that route:

$routeProvider.lookup('home_path'); //returns either the full hash or just the URL (or both)
$routeProvider.lookup('profile_path',{
  id : 'some_id',
  value : 'some_value'
}); //returns either the full hash or just the URL (or both)

Then you can attach this method to the $rootScope member and access the routes directly in the HTML (if you choose to do so).

$rootScope.r = function(path, args) {
  return $routeProvider.lookup(path, args);
};
<!--- This way you don't have to remember if you're using the hash or not --->
<a data-ng-href="{{ r('home_path') }}">...</a>
<a data-ng-href="{{ r('profile_path', { id : 'some_id', value : 'some_value' }) }}">...</a>

If this is OK with you guys I would happily contribute and put together the code and documentation. Is there anything in the works like this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions