修改根组件外的 DOM 个元素

Modify DOM elements outside root component

我的 Angular 2 应用程序的结构如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <base href="/" />

    <script src="jspm_packages/system.js"></script>
    <script src="config.js"></script>

    <script>
        System.import('client/main.js');
    </script>
</head>
<body>
    <my-app></my-app>
</body>
</html>

根组件是 my-app 组件。

我想修改根组件模板外部的 html 元素,以向其添加一些属性。这可能吗?

在我的例子中,我试图根据当前用户的设置设置 dirlang 属性,这些设置将在运行时检索。

您可以使用普通 JS DOM access or use the DOM adapter as shown in the Title service (source)

https://github.com/angular/angular/blob/master/CHANGELOG.md#400-beta0-2016-12-15 还引入了 Meta 服务来创建和修改 <meta> 标签。