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;
          }
}
2
  • How do you use title? Like this stackoverflow.com/questions/26308020/… ? Commented Feb 13, 2017 at 14:49
  • Yes using routeChangeSuccess - update code Commented Feb 13, 2017 at 14:53

1 Answer 1

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);
  }
})
Sign up to request clarification or add additional context in comments.

3 Comments

That title: 'Searching {{ textToSearch }}', doesn't seem to work
Should work, I tested ;) Make sure you inject $interpolation service. Do you have errors?
Ah yes forgot to inject the service!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.