1

Is there is any possible way to get the title attribute into header always when the route changed

Example:

when('/newsletter', {
            title: 'News Letter',
            templateUrl:'app/Views/segment/newsletter.html'
})

When this route comes the view is newsletter and i need to show the title as newsletter in Header Newsletter Tab

  • Yes. This should work. See [this](http://stackoverflow.com/questions/12506329/how-to-dynamically-change-header-based-on-angularjs-partial-view) for more details. Are you getting any errors? – Amarnath Dec 22 '15 at 07:02
  • Currently i am using $rootscope.title in each controller and your suggestion is what i am lookin for, Thanks a lot –  Dec 22 '15 at 07:05

2 Answers2

0

You can have a root scope variable named "title" and change it in your controller by doing $rootScope.title = 'new title'; Or better, you can have this done in the resolve option in router, for more information, please click: $routeProvider

rabbit.aaron
  • 2,369
  • 16
  • 25
0

What you should do is to expose $route service to the view in either main controller (topmost) or in run block (better):

app.run(['$route', '$rootScope', function($route, $rootScope) {
    $rootScope.$route = $route;
}]);

Then in any view you can display current title:

<header>
    <h1>{{ $route.current.title }}</h1>
</header>
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Uncaught TypeError: Cannot set property '$route' of undefined –  Dec 22 '15 at 07:31
  • It was my injector error this method is simple and good thanks @dfsq –  Dec 22 '15 at 07:33
  • to access to $rootscope from a view it is `

    {{ $root.$route.current.title }}

    ` or i am wrong ?
    – AlainIb Dec 22 '15 at 08:04
  • @AlainIb No, it's `$route.current.title`. – dfsq Dec 22 '15 at 08:15
  • Ok thanks. I always used in my project `$root.$route.current.title` style, maybe both work. for example if you have the same variable name in $rootscope and $scope – AlainIb Dec 22 '15 at 08:19
  • @AlainIb The reason why it works is because every scope object has a property reference `$root` pointing to the root scope. But in our case it's redundant. – dfsq Dec 22 '15 at 08:21