Modify DOM elements outside root component

那年仲夏 提交于 2019-12-20 06:11:06

问题


My Angular 2 app is structured something like this:

<!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>

The root component is the my-app component.

I would like to modify the html element which is outside the root component template to add some attributes to it. Is this possible?

In my case, I'm trying to set dir and lang attributes based on the current user's settings, which would be retrieved at runtime.


回答1:


You can either use normal 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 also introduces a Meta service to create and modify <meta> tags.



来源:https://stackoverflow.com/questions/36157605/modify-dom-elements-outside-root-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!