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?

6
  • you can read it here -> angular.io/guide/set-document-title Commented Mar 22, 2018 at 12:35
  • Is this for angular 4 ? Commented Mar 22, 2018 at 12:35
  • 2
    Here is a quick tip. If the domain is angular.io it means angular 2+ Commented Mar 22, 2018 at 12:36
  • index.html is not a template file for ProductComponent. It does not know what it is. Commented Mar 22, 2018 at 12:40
  • 1
    I'm voting to close this question as off-topic because the documentation explains how : angular.io/guide/set-document-title Commented Mar 22, 2018 at 12:43

3 Answers 3

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');
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
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[]'.
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'}

Comments

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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.