Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular using $routeParams in Page title

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.

like image 625
Alex Edwards Avatar asked Oct 02 '22 00:10

Alex Edwards


2 Answers

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...

like image 53
kah608 Avatar answered Oct 13 '22 10:10

kah608


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;
like image 27
Yuriy Kvartsyanyy Avatar answered Oct 13 '22 10:10

Yuriy Kvartsyanyy