Skip to content

Commit 2d41fd8

Browse files
authored
Merge pull request #14 from manthanank/new-changes
feat: added title service & its ex
2 parents d9d3bf9 + d4eb626 commit 2d41fd8

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

README.md

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

0 commit comments

Comments
 (0)