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?
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
$routeProviderservice.So lets say I have a route that goes to the home page:
And my HTML links like so:
This works fine, but if I change the route to from
/hometo 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.
Then by using either a service or factory you can have access to that route:
Then you can attach this method to the
$rootScopemember and access the routes directly in the HTML (if you choose to do so).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?