I have a similar route that should load a different view and controller based on whether or not the parameter is a number. Example:
/artists/2
sho
I'm using this as a solution for now and would be interested in alternatives!
1) Create a generic template that will load in a controller and view dynamically:
In this example I placed this view in /www/shared/dynamic-controller.html
2) Create a controller that checks the route params to determine which controller and view to load:
angular.module('appName').
controller('ArtistsDynamicRouteController', ['$scope', '$controller', '$routeParams', function($scope, $controller, $routeParams) {
if(/^\d+$/.test($routeParams.pageOrId)) {
// when pageOrId is a page (number) we want to load the ArtistsIndexController
$scope.controller = $controller('ArtistsIndexController', { $scope: $scope }).constructor;
$scope.templateUrl = '/www/artists/index.html';
} else {
// when pageOrId is an id (non-number) we want to load the ArtistsProfileController
$scope.controller = $controller('ArtistsProfileController', { $scope: $scope }).constructor;
$scope.templateUrl = '/www/artists/profile.html';
}
}]);
3) Use one route regardless of the parameter type:
// handles both /artists/2 and /artists/username
$routeProvider.when("/artists/:pageOrName", {
templateUrl: "/www/shared/dynamic-controller.html",
controller: "ArtistsDynamicRouteController"
});