2016-10-10 84 views
2

我想學習角度,所以我剛剛完成heroes tutorial角度承諾後設置標題

然後我想我會看看改變這個,以便我可以改變每頁的文檔標題。所以我按照說明添加了title service

這在我的儀表板頁面上可以正常工作,我可以簡單地在初始化時調用標題服務,所以我想我會嘗試將它添加到我的英雄詳細信息頁面,以查看是否可以使文檔標題成爲英雄的名字:

import { Component, Input, OnInit } from '@angular/core'; 
import { Title }     from '@angular/platform-browser'; 
import { ActivatedRoute, Params } from '@angular/router'; 
import { Location }     from '@angular/common'; 

import { Hero }      from '../classes/hero'; 
import { HeroService }    from '../services/hero.service'; 

@Component({ 
    selector: 'my-hero-details', 
    templateUrl: '/app/templates/hero-details.component.template.html' 
}) 

export class HeroDetailsComponent implements OnInit { 
    @Input() 
    hero: Hero; 

    constructor(
     private heroService: HeroService, 
     private route: ActivatedRoute, 
     private location: Location, 
     private titleService: Title 
    ) { } 

    public ngOnInit(): void { 
     this.route.params.forEach((params: Params) => { 
      let id = +params['id']; 

      this.heroService.getHero(id) 
       .then(hero => this.hero = hero) 
       .then(function() { this.setTitle(this.hero.name); }); // this is the line I have added 
     }); 
    } 

    public save(): void { 
     this.heroService.update(this.hero) 
      .then(() => this.goBack()); 
    } 

    public delete(): void { 
     this.heroService 
      .delete(this.hero.id) 
      .then(() => this.goBack()); 
    } 

    public goBack(): void { 
     console.log('back') 
     this.location.back(); 
    } 

    private setTitle(newTitle: string) { 
     this.titleService.setTitle(newTitle); 
    } 
} 

正如你可以從我的代碼中看到的,我想設置的主人公許運行後設置標題。然而,似乎沒有任何事情發生 - 不會出現錯誤,標題也不會改變。

我在這裏做錯了什麼?我不會那樣連鎖嗎?

回答