Angular 2.x更改< title>在头部(我的应用程序之外) [英] Angular 2.x change <title> in head (outside my app)

查看:92
本文介绍了Angular 2.x更改< title>在头部(我的应用程序之外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为应该很容易,但我找不到。

I think it should be easy but I cannot find how.

我有类似的东西

<html>
<head>
  <title>{{'a' + 'b'}}</title>
</head>
<body>
  <my-app>Loading...</my-app>
</body>
</html>

好像我无法访问 my-app

在角度1.x中很容易,我能够添加 ng-app 任何元素(< html ng-app =myApp> )。

In angular 1.x it was easy, I was able to add ng-app on any element (<html ng-app="myApp">).

现在我想我我只能在体内引导。

Now I think I'm able only bootstrap in body.

我知道我可以手动引导某种方式(还没试过),但动态更改单页应用程序中的标题应该是超级简单的,不应该吗?

I know I can manually bootstrap somehow (didn't try yet), but dynamically change title in single page applications should be super-easy, shouldn't it?

推荐答案

Angular2无法自举到整个html。但您可以使用标题服务

Angular2 can't be bootstrapped to entire html. But you can use Title Service.


可用于获取和设置当前HTML文档标题的服务。

A service that can be used to get and set the title of a current HTML document.

它有2种方法:

getTitle()

setTitle()

不要忘记检查依赖注入部分,了解如何使用这些服务。

Don't forget to check the dependency injection section out to see how you can use the services.

编辑:

截至发布时间(2.0.0),您可以这样做:

As of the release (2.0.0), this is how you can do it:

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

export class SomeComponent {
  constructor(title: Title) {
    // title.getTitle();
    // title.setTitle('new title');
  }
}

的文档标题服务现在在这里: https://angular.io/docs/ts/latest/api/platform-b​​rowser/index/Title-class.html

这篇关于Angular 2.x更改&lt; title&gt;在头部(我的应用程序之外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆