Can angularjs routes have optional parameter values?
Please see @jlareau answer here: https://stackoverflow.com/questions/11534710/angularjs-how-to-use-routeparams-in-generating-the-templateurl
You can use a function to generate the template string:
var app = angular.module('app',[]);
app.config(
function($routeProvider) {
$routeProvider.
when('/', {templateUrl:'/home'}).
when('/users/:user_id',
{
controller:UserView,
templateUrl: function(params){ return '/users/view/' + params.user_id; }
}
).
otherwise({redirectTo:'/'});
}
);
It looks like Angular has support for this now.
From the latest (v1.2.0) docs for $routeProvider.when(path, route)
:
path
can contain optional named groups with a question mark (:name?
)
Like @g-eorge mention, you can make it like this:
module.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/users/:userId?', {templateUrl: 'template.tpl.html', controller: myCtrl})
}]);
You can also make as much as u need optional parameters.