@@ -2001,6 +2001,152 @@ Angular's animation system is built on CSS functionality in order to animate any
20012001
20022002## Meta tags
20032003
2004+ Title Service
2005+
2006+ ` ` ` jsx
2007+ import { BrowserModule , Title } from ' @angular/platform-browser' ;
2008+ ` ` `
2009+
2010+ ` ` ` jsx
2011+ @NgModule ({
2012+ declarations: [
2013+ AppComponent
2014+ ],
2015+ imports: [
2016+ BrowserModule,
2017+ AppRoutingModule
2018+ ],
2019+ providers: [Title],
2020+ bootstrap: [AppComponent]
2021+ })
2022+ export class AppModule { }
2023+ ` ` `
2024+
2025+ ` ` ` jsx
2026+ export class TitleComponent implements OnInit {
2027+ constructor (private title : Title ) { }
2028+ }
2029+ ` ` `
2030+
2031+ ` ` ` jsx
2032+ ngOnInit () {
2033+ this .title .setTitle (" Learn Angular" )
2034+ }
2035+ ` ` `
2036+
2037+ ` ` ` jsx
2038+ import { Component , OnInit } from ' @angular/core' ;
2039+ import { Title , MetaDefinition } from ' @angular/platform-browser' ;
2040+
2041+ @Component ({
2042+ template: ` <h1>App Component</h1>`
2043+ })
2044+ export class AppComponent implements OnInit {
2045+ title = ' App Component' ;
2046+
2047+ constructor (private title : Title ){
2048+ }
2049+
2050+ ngOnInit () {
2051+ this .title .setTitle (" Learn Angular" )
2052+ }
2053+
2054+ }
2055+ ` ` `
2056+
2057+ Title Service
2058+
2059+ ` ` ` jsx
2060+ // app.module.ts
2061+ import { BrowserModule , Title } from ' @angular/platform-browser' ;
2062+ import { NgModule } from ' @angular/core' ;
2063+
2064+ import { AppRoutingModule } from ' ./app-routing.module' ;
2065+ import { AppComponent } from ' ./app.component' ;
2066+ import { HomeComponent } from ' ./home.component' ;
2067+
2068+ @NgModule ({
2069+ declarations: [
2070+ AppComponent, HomeComponent
2071+ ],
2072+ imports: [
2073+ BrowserModule,
2074+ AppRoutingModule
2075+ ],
2076+ providers: [Title],
2077+ bootstrap: [AppComponent]
2078+ })
2079+ export class AppModule { }
2080+ ` ` `
2081+
2082+ ` ` ` jsx
2083+ // app-routing.module.ts
2084+ import { NgModule } from ' @angular/core' ;
2085+ import { Routes , RouterModule } from ' @angular/router' ;
2086+ import { HomeComponent } from ' ./home.component' ;
2087+
2088+ const routes: Routes = [
2089+ {path: ' home' , component: HomeComponent},
2090+ ];
2091+
2092+ @NgModule ({
2093+ imports: [RouterModule .forRoot (routes)],
2094+ exports: [RouterModule]
2095+ })
2096+ export class AppRoutingModule { }
2097+ ` ` `
2098+
2099+ ` ` ` jsx
2100+ // app.component.ts
2101+ import { Component } from ' @angular/core' ;
2102+ import { Title } from ' @angular/platform-browser' ;
2103+
2104+ @Component ({
2105+ selector: ' app-root' ,
2106+ templateUrl: ' ./app.component.html' ,
2107+ styleUrls: [' ./app.component.css' ]
2108+ })
2109+ export class AppComponent {
2110+ title = ' Title Service Example' ;
2111+
2112+ constructor (private titleService : Title ) {
2113+ }
2114+
2115+ ngOnInit () {
2116+ this .titleService .setTitle (this .title );
2117+ }
2118+ }
2119+ ` ` `
2120+
2121+ ` ` ` html
2122+ <!-- app .component .html -->
2123+ < h1> Title Service Example< / h1>
2124+
2125+ < ul>
2126+ < li>< a [routerLink]= " ['/home']" > Home< / a> < / li>
2127+ < / ul>
2128+
2129+ < router- outlet>< / router- outlet>
2130+ ` ` `
2131+
2132+ ` ` ` typescript
2133+ // home.component.ts
2134+ @Component ({
2135+ template: ` <h1>Home Component</h1>`
2136+ })
2137+ export class HomeComponent implements OnInit {
2138+ title = ' Home Component Title' ;
2139+
2140+ constructor (private titleService : Title ){
2141+ }
2142+
2143+ ngOnInit () {
2144+ this .titleService .setTitle (this .title );
2145+ }
2146+
2147+ }
2148+ ` ` `
2149+
20042150Meta Service
20052151
20062152` ` ` jsx
0 commit comments