1

In the index.html within the "src" folder, I wrote {{ title }} like as follows:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>{{ title }}</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
  <app-root></app-root>
</body>
</html>

In product.components.ts, I defined the title as "Products" as follows, but it wont works.

export class ProductComponent implements OnInit {
  products: Product[] = [];
  dataInvalid = false;
  formErrors = [];
  formSubmitting = false;
  title = 'Products';

How can I change the title if users clicks on different pages?

Roland Rácz
  • 2,879
  • 3
  • 18
  • 44

3 Answers3

2

You have to inject Title from platform-browser in your component

import {Title} from "@angular/platform-browser";

constructor(private title: Title) {
            this.title.setTitle('Your title');
}
HMarteau
  • 654
  • 4
  • 18
  • 1
    You can put this in all your child components (your pages if you prefer: here it is product.component.ts) And each will update the title – HMarteau Mar 22 '18 at 12:56
  • I received the following error message during compilation: ERROR in src/app/product/product.component.ts(27,30): error TS2339: Property 'data' does not exist on type 'Product[]'. src/app/product/product.component.ts(28,34): error TS2339: Property 'page_title' does not exist on type 'Product[]'. – Niladri Banerjee - Uttarpara Mar 27 '18 at 12:27
1

just add into the routing. so you no need inject title service in every component. for Ex

 path: ':path',
 component: ExampleComponent,
 data: {title:'title'}
Shailesh Ladumor
  • 7,052
  • 5
  • 42
  • 55
0

Now you can set the title tag and meta tag with anglar. Just use the services provide into @angular/platform-browser Here we go ...

import { Title, Meta }     from '@angular/platform-browser';

@Component({
selector: 'app-root',
template:
  `My component
  `
})
export class YourComponent {
  public constructor(private titleService: Title, private meta: Meta ) {
     this.setTitle('My title');
     this.setDynamicTitle();
     this.setMetaTags()
  }

  public setTitle( newTitle: string) {
    // each component
    this.titleService.setTitle( newTitle ); 
  }
  public setDynamicTitle() {
     // via service
    this.yourService.get('api/url').subscribe((data) => {
       this.titleService.setTitle( data.title );  
    })
  }
  public setMetaTags() {
      this.meta.addTags([{
          name: 'description',
          content: 'Your description'
        }, {
          name: 'author',
          content: 'You'
        }
      ])
  }
}

Here another reference: How to change title of a page using angular(angular 2 or 4) route?

Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60