I found this answer in another SO question. But i would like to also use route parameters in some of the page titles.
If i have a $routeProvider
like this:
$routeProvider.when('/demo/:name/:idNumber', {title : ' - :name', templateUrl: 'partials/details.html', controller: 'AppDetails'});
how do I match the :name
in the title to the :name
in the location? I tried the following
.run(['$location', '$rootScope', '$routeParams', function($location, $rootScope, $routeParams) {
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
if(current.$$route.title.contains(":")){
//Not sure how to get the route param using the title param string
}else{
$rootScope.title = current.$$route.title;
}
});
}]);
but I don't know how to get a variable from the routeParams using a string.
Can't you just access it like this:
$routeParams[$routeParams.title]
I think that is what you are asking, but I'm not quite sure...
In the routeChangeSuccess handler I added
var title = currentRoute.$$route.title;
if (title && $routeParams){
for(var paramName in $routeParams) {
title = title.replace(new RegExp(':' + paramName, 'g'), $routeParams[paramName]);
}
}
$rootScope.title = title;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With