【问题标题】:How to update theme-color in application like angular如何在 Angular 等应用程序中更新主题颜色
【发布时间】:2019-03-17 18:55:56
【问题描述】:

我们如何使用 Angular 4 应用程序在 head 标签内动态更新主题颜色。

<meta name="theme-color" content="#db5945">

【问题讨论】:

    标签: angular meta


    【解决方案1】:

    编辑: Debmallya Bhattacharya 提出了更好的解决方案:

    使用updateTag from the Angular Meta service

    this.meta.updateTag({ content: color }, 'name=theme-color');
    

    Running example

    旧答案: 我只需为此创建一个服务并执行一些 javascript (javascript code got from here):

    import { Injectable } from '@angular/core';
    
    @Injectable({
      providedIn: 'root'
    })
    export class MetaColorService {
      changeThemeColor(color: string) {
        const metaThemeColor = document
        .querySelector("meta[name=theme-color]");
        metaThemeColor.setAttribute("content", color);
      }
    }
    

    Running test to run on mobile device.

    Editable version of the example.

    【讨论】:

    猜你喜欢
    • 2018-09-15
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 2019-04-26
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多