简体   繁体   English

Angular 6 - 有条件地更改HTML标头中元素的属性

[英]Angular 6 - Conditionally change Attributes of Element in HTML Header

I recently found out that you can alter Chromes header on Android by putting this in the HTML header. 我最近发现你可以通过将它放在HTML标题中来改变Android上的Chromes标题。

<meta name="theme-color" content="#1ee078">

This works perfectly. 这非常有效。 But I want the content color to be dynamic based on the current theme. 但我希望content颜色是基于当前主题的动态。

How would you make the color of this meta tag dynamic, if you would have the current theme color as a variable in eg the AppComponent ? 如果您将当前主题颜色作为变量(例如AppComponent ,您将如何使此meta标记的颜色动态AppComponent

Within Angular there is a built in API to edit meta tags. 在Angular中,有一个内置的API来编辑meta标记。 @angular/platform-browser/meta

You can use the updateTag function. 您可以使用updateTag函数。

You should be able to do something like; 你应该能够做类似的事情;

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

constructor(
  private meta: Meta
) { }

ngOnInit() {
  this.meta.updateTag({ 
    name: 'theme-color', 
    content: '#1ee078' 
  });
}

You could try this one. 你可以尝试这个。 Depending on your page, it might work. 根据您的页面,它可能会有效。 :) :)

var meta=document.createElement('meta');
meta.name='theme-color';

meta.setAttribute('content', '#1ee078');

document.getElementsByTagName('head')[0].appendChild(meta);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM