templateUrl
can be set as a function to handle dynamic routing values.
In accordance with the documentation:
"If templateUrl is defined as a function, it will receive the following parameters:
{Array.} - route parameters obtained from the current $location.path() by utilizing the current route"
This function can be used for conditional logic similar to middleware, or for creating and returning a dynamic template using a separate logic function.
$routeProvider
.when('/', {
templateUrl: _ => { //no dynamic file name used here
let route = 'app/views/pages/';
if (user) route += 'home-page.html';
else if (admin) route += 'admin-page.html';
return route;
}
})
You have the option to further enhance this by incorporating parameters that introduce a dependency to your URL. In this case, adjustments may be required in the middleware as both are accepting the same value. You might need to modify the function to verify permissions on multiple levels, or implement distinct URL scopes.
An example involving $http
. Since I lack insight into your complete setup, assume .isUser
, isAdmin
, and url/for/permissions
require customization.
$http.get('/url/for/permissions/').then(results => {
$routeProvider
.when('/', {
templateUrl: _ => { //no dynamic file name used here
let route = 'app/views/pages/';
if (results.isUser) route += 'home-page.html';
else if (results.isAdmin) route += 'admin-page.html';
return route;
}
})
});