How to get page title using typescript?

Viewed 7073

I need to take page title and write it to a variable. I am using typescript code:

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'home',
    templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {

    title: string;

    ngOnInit() {
        console.log("Title is " + this.title);
        this.title === document.title;
    }
}

But I am getting "Title is undefined" on a console.

I also tried:

 title: string;

    ngOnInit() {
        this.title === this.titleService.getTitle();
        console.log("Title is " + this.title);
    }
    public constructor(private titleService: Title) { }
    getTitle() {
        this.titleService.getTitle();
    }

but the result is the same. What is the correct way to get page title?

2 Answers
Related