I am trying to change the page title from the router, can this be done?
import {RouteConfig} from 'angular2/router'; @RouteConfig([ {path: '/home', component: HomeCmp, name: 'HomeCmp' } ]) class MyApp {}
We use SetTitle to set the title of the page and GetTitle to find out the current title of the page.
A service that can be used to get and set the title of a current HTML document. class Title { getTitle(): string setTitle(newTitle: string) }
ActivatedRoutelink. Provides access to information about a route associated with a component that is loaded in an outlet. Use to traverse the RouterState tree and extract information from nodes.
ActivatedRoute Contains the information about a route associated with a component loaded in an outlet. It can also be used to pass data from one component to another component using route such as Id, flag, state etc.
The Title
service @EricMartinez points out has a setTitle()
method - that's all you need to set the title.
In terms of doing it automatically on route changes, as of now there's no built-in way of doing this other than subscribing to Router
and calling setTitle()
in your callback:
import {RouteConfig} from 'angular2/router'; import {Title} from 'angular2/platform/browser'; @RouteConfig([ {path: '/home', component: HomeCmp, name: 'HomeCmp' } ]) class MyApp { constructor(router:Router, title:Title) { router.events.subscribe((event)=>{ //fires on every URL change title.setTitle(getTitleFor(router.url)); }); } }
That said, I emphasize as of now because the router is still under heavy development, and I expect (or at least hope) that we'll be able to do this via RouteConfig
in the final release.
EDIT:
As of the release of Angular 2 (2.0.0), a few things have changed:
Title
service are now here: https://angular.io/docs/ts/latest/api/platform-browser/index/Title-class.html '@angular/platform-browser'
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