i am trying to implement routing in angularjs. But i wish to do it in a the same way as is done in cakephp. for eg: in cakephp if i pass something like
blog/manager/users/index/page:2/sort:username/direction:asc
or
blog/manager/users/index/sort:username/direction:asc/page:2
both generate the same result. i.e. cakephp understands the named parameters passed in the url in any order (order doesn't matter). What if i need to do the same in angularjs? For now i have following coding for my routing controller in angularjs:
angular.module('productapp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/productapp', {templateUrl: 'partials/productList.html', controller: productsCtrl}).
when('/productapp/:productId', {templateUrl: 'partials/edit.html', controller: editCtrl}).
otherwise({redirectTo: '/productapp'});
}],
['$locationProvider', function($locationProvider) {
$locationProvider.html5Mode = true;
}]);
Do i need to hardcode each and every combination of the named parameter? or is there a solution for this?
You can try this:
when('/blog/manager/users/index/:param1/:param2/:param2, {templaeURL: 'mytmpl', controller: MyCtrl}).
now param1,param2 and param3 can receive anything, but if you want to do something with the parameters you have to validate in your controller (eg: MyCtrl).
In PHP (or any backend language), you can be that flexible with your URLs because of mod_rewrite and the $_GET
object. In JavaScript, we have to add all of that extra logic that's given to us for free.
Using your example above with the blog URLs, here's how I'd write those in Angular:
$routeProvider.
when('blog/manager/users/index/:page/:sort/:direction', {
controller: blogController
}).
when('blog/manager/users/index/:sort/:direction/:page', {
controller: blogController
});
If you wish not to create a route for each possible combination and order of parameters, I suggest looking into the resolve
parameter or writing a custom params function that watches the routing events.