我正在使用这个加载指令作为一个按钮.我的问题是,在使用该指令时,它会将进度加载器附加到按钮下方,而不是按钮内部.

我已经通过控制台记录了this.elementRef.nativeElement,它返回了正确的按钮元素.

button-loader.directive.ts

import { Directive, ElementRef, Input, ViewContainerRef, Renderer2 } from '@angular/core';
import { MatButton } from '@angular/material/button';
import { MatProgressBar } from '@angular/material/progress-bar';

@Directive({
  selector: '[buttonLoader]'
})
export class ButtonLoaderDirective {
  progressElement: any;

  @Input() set buttonLoader(value: boolean) {
    this.toggle(value);
  }

  constructor(
    private viewContainerRef: ViewContainerRef, 
    private matButton: MatButton, 
    private renderer: Renderer2, 
    private elementRef: ElementRef) {
    this.loadComponent();
  }

  loadComponent() {
    this.viewContainerRef.clear();
    
    // Create Component
    const matProgress = this.viewContainerRef.createComponent(MatProgressBar);
    
    matProgress.instance.mode = 'indeterminate';
    
    // Move it
    this.progressElement = matProgress.injector.get(MatProgressBar)._elementRef.nativeElement
    
    this.renderer.appendChild(this.elementRef.nativeElement, this.progressElement);


    // Add custom class
    this.elementRef.nativeElement.classList.add('mat-load-button');
  }

  toggle(condition: boolean) {
    condition ? this.show() : this.hide()
  }

  show() {
    this.progressElement.style.opacity = '0.7';
    this.matButton.disabled = true;
  }

  hide() {
    this.progressElement.style.opacity = '0';
    this.matButton.disabled = false;
  }

}

HTML

<button 
    [buttonLoader]="true"
    mat-raised-button 
    color="primary"
>
Save
</button>

Currently how it looks

enter image description here

enter image description here

Here's how I want it to look

enter image description here

enter image description here

推荐答案

你的指令奏效了.progressbar人有正确的"父母".我只将progressbar样式修改为position: absolute;和当前位置+宽度:

Updated directive

import { Directive, ElementRef, Input, ViewContainerRef, Renderer2 } from '@angular/core';
import { MatButton } from '@angular/material/button';
import { MatProgressBar } from '@angular/material/progress-bar';

@Directive({
  selector: '[buttonLoader]'
})
export class ButtonLoaderDirective {
  progressElement: any;

  @Input() set buttonLoader(value: boolean) {
    this.toggle(value);
  }

  constructor(
    private viewContainerRef: ViewContainerRef, 
    private matButton: MatButton, 
    private renderer: Renderer2, 
    private elementRef: ElementRef) {
    this.loadComponent();
  }

  loadComponent() {
    this.viewContainerRef.clear();
    
    // Create Component
    const matProgress = this.viewContainerRef.createComponent(MatProgressBar);
    
    matProgress.instance.mode = 'indeterminate';
    
    // Move it
    this.progressElement = matProgress.injector.get(MatProgressBar)._elementRef.nativeElement

    this.renderer.appendChild(this.elementRef.nativeElement, this.progressElement);


    // Add custom class
    this.elementRef.nativeElement.classList.add('mat-load-button');
  }

  toggle(condition: boolean) {
    condition ? this.show() : this.hide()
  }

  show() {
    this.progressElement.style.opacity = '0.7';
    this.progressElement.style.position = "absolute"
    this.matButton.disabled = true;
    this.progressElement.style.bottom = "10px";
    this.progressElement.style.width = "calc(100% - 10px)"

  }

  hide() {
    this.progressElement.style.opacity = '0';
    this.matButton.disabled = false;
  }
}

HTML

  <button
    style="height: 80px"
    mat-raised-button
    color="primary"
    [buttonLoader]="true"
  >
    My Button
  </button>

以下是完整的Stackblitz个示例.

下面是结果:

enter image description here

Angular相关问答推荐

文件阅读器未正确给出某些CSV文件的结果

升级到angular 17并转换为独立的加载器拦截器后,

使用Angular 13的NgxPrinterService打印时,第二页上的空白区域

独立组件;依赖项注入

倒计时计时器,每10秒重新启动一次,共4次

P-DropDown不会使用react 式表单手动设置Value

在以下位置升级到Angular 16 Break Sharepoint广告:This._history.replaceState不是函数

错误:服务或构建Angular 项目时模块构建失败

如何在 form.value 中禁用复选框

无法获得 angular-material md-sidenav 的 100% 高度

Angular 6 - NullInjectorError:单元测试中没有 HttpClient 的提供者

如何在 Angular CLI 1.1.1 版中将路由模块添加到现有模块?

如何在 Angular 6 中使用 mouseover 和 mouseout

Angular 2 filter/search 列表

为什么用?模板绑定中的运算符?

Angular2中是否有像window.onbeforeunload这样的生命周期钩子?

从 Angular 中的自定义表单组件访问 FormControl

NPM 脚本 start退出,但未指示 Angular CLI 正在侦听请求

为什么 Angular 项目中没有 Angular-CLI 为 model生成命令?

Angular2:将数组转换为 Observable