Currently, I am utilizing $routeProvider in order to switch between pages (templates) and controllers when a user clicks on a link.
Here's an example of how it's done:
$routeProvider.when('/profile/', {
templateUrl: '/app/views/profile.html',
controller: 'ProfileCtrl'
}).when('/timeline/', {
templateUrl: '/app/views/timeline.html',
controller: 'TimelineCtrl'
}).when('/chat/:username', {
templateUrl: function(param){
if(param){
return '/app/views/chat.html?' + param;
}
return '/';
},
controller: 'ChatCtrl'
}).otherwise({ redirectTo: '/' });
The issue I'm facing is that there are numerous pages in my application, requiring me to register each URL in the .when
condition repeatedly. Additionally, the template URL and controller name are based on the path of the link.
My question is: Is it possible to consolidate all these individual conditions into one single statement?
Something like this:
$routeProvider.when(url, {
templateUrl: '/app/views/' + url + '.html',
controller: url + 'Ctrl'
});
Thank you in advance :)