【问题标题】:angular material design change color with directive角度材料设计随指令改变颜色
【发布时间】:2018-05-09 00:15:49
【问题描述】:

我一直在尝试更改材料设计组件的颜色,但当我将指令放在组件上但没有任何更改时,我将它放在 html 元素上,那里也没有发生任何事情。

这里是服务:

import { Injectable, Inject, Optional } from '@angular/core';
import { ColorInterface } from "../core/color.interface"

@Injectable()
export class ColorService {
  // Colors
  private primary: string = "#212121";
  private primaryDark: string = "#000000";
  private primaryLight: string = "#484848";
  private accent: string = "#9e9e9e";
  private accentDark: string = "#707070";
  private accentLight: string = "#cfcfcf";
  private warn: string = "#ffc107";
  private success: string = "#4caf50";
  private danger: string = "#f44336";

  // Set Colors On Load
  constructor(@Optional() @Inject('color') colors: ColorInterface) {
    if(colors) {
      this.primary = colors.primary;
      this.primaryDark = colors.primaryDark;
      this.primaryLight = colors.primaryLight;
      this.accent = colors.accent;
      this.accentDark = colors.accentDark;
      this.accentLight = colors.accentLight;
      this.warn = colors.warn;
      this.success = colors.success;
      this.danger = colors.danger;
    }
  }

  ////  Get Colors  ////

  public get primaryColor(): string {
    return this.primary;
  }
  public get primaryDarkColor(): string {
    return this.primaryDark;
  }
  public get primaryLightColor(): string {
    return this.primaryLight;
  }
  public get accentColor(): string {
    return this.accent;
  }
  public get accentDarkColor(): string {
    return this.accentDark;
  }
  public get accentLightColor(): string {
    return this.accentLight;
  }
  public get successColor(): string {
    return this.success;
  }
  public get warnColor(): string {
    return this.warn;
  }
  public get dangerColor(): string {
    return this.danger;
  }



}

服务所做的是从设置模块(通常是应用模块)获取输入,然后分配私有变量,因此调用它们的唯一方法是通过 getter。

指令如下:

import { Directive, Input, ElementRef, Renderer} from '@angular/core';
import { ColorService } from "../services/color.service";

@Directive({
  selector: '[quiColor]'
})
export class ColorDirective {
  @Input() quicolor: string;

  constructor(
    private element: ElementRef,
    private render: Renderer,
    private color: ColorService)
  {
    if (this.quicolor == "primary")
  this.element.nativeElement.style.backgroundColor = color.primaryColor;
    if (this.quicolor == "darkprimary")
this.element.nativeElement.style.backgroundColor = color.primaryDarkColor;
    if (this.quicolor == "lightprimary")
this.element.nativeElement.style.backgroundColor= color.primaryLightColor;
    if (this.quicolor == "accent")
this.element.nativeElement.style.backgroundColor = color.accentColor;
    if (this.quicolor == "darkaccent")
this.element.nativeElement.style.backgroundColor = color.accentDarkColor;
    if (this.quicolor == "lightaccent")
this.element.nativeElement.style.backgroundColor = color.accentLightColor;
    if (this.quicolor == "warn")
this.element.nativeElement.style.backgroundColor = color.warnColor;
    if (this.quicolor == "success")
this.element.nativeElement.style.backgroundColor = color.successColor;
    if (this.quicolor == "danger")
this.element.nativeElement.style.backgroundColor = color.dangerColor;
  }

}

组件如下:

<mat-toolbar [quicolor]="'primary'">hello</mat-toolbar>
<p [quicolor]="'primary'">Test 2</p>

任何帮助将不胜感激。

【问题讨论】:

    标签: angular material-design angular-material angular2-directives


    【解决方案1】:

    试试这个代码

    <mat-toolbar quicolor [quicolor]="'primary'">hello</mat-toolbar>
    <p quicolor [quicolor]="'primary'">Test 2</p>
    

    查看颜色指令示例here了解更多详情

    【讨论】:

    • 不幸的是,它并没有完全解决这个问题,但是它通过一个错误来解决这个问题:错误:模板解析错误:无法绑定到 'quicolor',因为它不是 ' 的已知属性垫工具栏'。
    • 我决定将指令拆分为多个指令,这似乎有效
    • [] 用于binf输入。我认为您需要使用不带 [] 的指令。试试 quicolor="primary" 或 quicolor="'primary'"
    • 抱歉你做对了angular.io/guide/…
    猜你喜欢
    • 2019-02-28
    • 2021-09-19
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2016-12-02
    • 2020-01-15
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多