如何通过不在app.component.ts进行更改来更改Angular2页面的标题?

时间:2017-03-21 06:22:29

标签: angular java-ee typescript

当我从Angular2论坛阅读时,我们可以通过在app.component.ts和app.module.ts上进行更改来更改html页面的标题。有没有办法在单个页面上进行此更改而不对app.component.ts进行任何更改?因为我有太多的组件,我不想在所有页面或组件上显示标题名称更改?我可以对单个组件的ts进行更改吗?你能为我提供样品吗?

此致 阿尔珀

1 个答案:

答案 0 :(得分:2)

<强>的AppModule

import { NgModule } from '@angular/core';
import { BrowserModule, Title }  from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
  imports: [
    BrowserModule
  ],
  declarations: [
    AppComponent
  ],
  providers: [
    Title
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

<强> AppComponent

export class AppComponent {
  public constructor(private titleService: Title ) { }
  public setTitle( newTitle: string) {
    this.titleService.setTitle( newTitle );
  }
}