问题
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