1

How can use the optional parameter from my angular route in the title.

So I want textToSearch to appear in the title:

.when('/search/:textToSearch',
    {
      title: 'Searching' + textToSearch,
      templateUrl: 'views/search.html',
      controller: 'searchController'
    }
  )

And i'm setting title in routeChangeSuccess:

$rootScope.$on('$routeChangeSuccess', function() {
          //Set the <title> tag
          if (!$route.current.title) {
            document.title = 'Default Generic Title';
          } else {
            document.title = $route.current.title;
          }
}
userMod2
  • 8,312
  • 13
  • 63
  • 115
  • How do you use `title`? Like this http://stackoverflow.com/questions/26308020/how-to-change-page-title-in-angular-using-routeprovider/26308083#26308083 ? – dfsq Feb 13 '17 at 14:49
  • Yes using routeChangeSuccess - update code – userMod2 Feb 13 '17 at 14:53

1 Answers1

1

I would use a placeholder for your route params in title string and interpolate it against route params object:

.when('/search/:textToSearch', {
  title: 'Searching {{ textToSearch }}',
  templateUrl: 'views/search.html',
  controller: 'searchController'
})

Interpolation part (make sure to inject $interpolate service):

$rootScope.$on('$routeChangeSuccess', function() {
  //Set the <title> tag
  if (!$route.current.title) {
    document.title = 'Default Generic Title';
  } else {
    document.title = $interpolate($route.current.title)($route.current.params);
  }
})
dfsq
  • 191,768
  • 25
  • 236
  • 258