角度7:通过指令在按钮上添加mat-raised按钮

问题描述 投票:0回答:1

我坚持使用该指令。尝试通过自定义指令为按钮添加“ mat-raised-button”和“ color”。但这没有用。请帮助我工作。代码在这里,

import { Directive, ElementRef, Renderer2, HostListener, AfterViewInit } from '@angular/core';
import { MatRipple } from '@angular/material';
import { MatRipple } from '@angular/material';
@Directive({
    selector: '[appButton]',
    providers: [MatRipple]
})
export class ButtonDirective{
    constructor(el: ElementRef, renderer: Renderer2, ripple: MatRipple) {
        renderer.setAttribute(el.nativeElement, 'mat-raised-button', '');
        renderer.setAttribute(el.nativeElement, 'color', 'primary');
    }

}
angular angular-material angular7
1个回答
0
投票

放置属性,但是如上所述,它不会初始化预期的样式:

import { Directive, ElementRef, Renderer2 } from "@angular/core";

@Directive({
  selector: '[appButton]'
})
export class ButtonDirective {
  constructor(
    private _elRef: ElementRef,
    private _renderer: Renderer2,
  ) {
    this._renderer.setAttribute(this._elRef.nativeElement, 'mat-raised-button', '');
    this._renderer.setAttribute(this._elRef.nativeElement, 'color', 'primary');
  }
}

有任何建议或新闻吗? (使用角/角材料9)

© www.soinside.com 2019 - 2024. All rights reserved.