如何从材料角度应用样式



我需要添加一个带有材料Angular 2样式的按钮。所以我有一个执行此操作的按钮:

<button mat-button (click)="addElement($event, 'button')">Add button</button>

定义加法的地方:

addElement(ev, tag) {
    const htmlElem = ev.currentTarget;
    const el = this.renderer.createElement(tag);
    this.renderer.setAttribute(el, 'mat-button', '');
    el.textContent = 'New Button';
    htmlElem.parentNode.insertBefore(el, null);
  }

单击按钮,然后在我的HTML中创建一个新元素,显示为:

<button mat-button">Add button</button>

函数代码正确生成了按钮,但是它并不像材料代码的原始按钮代码中所示所有孩子

所示

那么,我是否有办法"刷新"该按钮,以便标准MAT-Button的所有内容都适用于通过JS代码添加的按钮?

出了什么问题?

您添加了Mat-button属性是对的

以下是Mat-button的整体样式和结构

<button class="mat-button" mat-button="">
<span class="mat-button-wrapper">Add button</span>
<div class="mat-button-ripple mat-ripple"></div>
<div class="mat-button-focus-overlay"></div>
</button>

您可以看到MAT按钮中还有其他HTML元素,其中包括连锁效果和焦点覆盖。


解决您的问题的解决方案

我们将使用Angular Renderer创建HTML元素,将属性设置为元素并将其附加在元素上。

所以我们做了什么

创建将触发附加的按钮

<button id="clickBtn" (click)="onClick()">Click here to add Button</button>

在组件中导入导入Directive, ElementRef, Renderer2

{ Component, Directive, ElementRef, Renderer2 } from '@angular/core';

添加一个指令,该指令将针对html元素附加在该按钮上(#clickbtn [是我们创建的按钮的ID标签(

(
@Directive({ 
     selector: '#clickBtn' 
})

创建一个构造函数以注入渲染器和ElementRef

constructor(private renderer: Renderer2,private elRef: ElementRef)   { 
}

触发点击事件以附加按钮

    onClick() {
     const btn = this.renderer.createElement('button');
     const span = this.renderer.createElement('span');
     const div1 = this.renderer.createElement('div');
     const div2 = this.renderer.createElement('div');
     const text = this.renderer.createText('I am a Generated Button');
     const attrBtn = this.renderer.setAttribute(btn, 'class', 'mat-button');
    const attrSpan = this.renderer.setAttribute(span, 'class', 'mat-button-wrapper');
    const attrDiv1 = this.renderer.setAttribute(div1, 'class', 'mat-button-ripple mat-ripple');
    const attrDiv2 = this.renderer.setAttribute(div2, 'class', 'mat-button-focus-overlay');
     this.renderer.appendChild(span, text);
     this.renderer.appendChild(btn, span);
     this.renderer.appendChild(btn, div1);
     this.renderer.appendChild(btn, div2);
     this.renderer.appendChild(this.elRef.nativeElement, btn);
   }

哇,这是怎么回事。如您所见,我们在这里生成Mat-Button的所有结构

要了解有关Renderer2的更多信息,请访问此链接。

https://alligator.io/angular/using-renderer2/


请参阅stackblitz上的实时代码链接

https://stackblitz.com/edit/dmgrave-ng-so-answer-dom?file=papp/app.component.ts

希望这会有所帮助。

我不确定为什么要动态生成按钮,但我会做这样的事情

在您的模板中:

<button mat-raised-button (click)="addNewButton()">Add new </button>
<div  *ngFor="let btn of buttonsList">
<button mat-raised-button color="primary">
  {{btn+1}}
</button>
</div>

在您的组件中:

export class AppComponent  {
  buttonsList = [];
  addNewButton():void{
    const newId = this.buttonsList.length;
    this.buttonsList.push(newId);
  }
}

您只需要循环遍历数组并显示按钮即可。每次添加新按钮时,您只需要向阵列推一个新值,然后让框架进行繁重的升降即可。

这是Stackblitz演示:https://stackblitz.com/edit/angular-7rfwrx?file=papp/app.component.ts

希望这会有所帮助。

最新更新