【问题标题】:Angular 2 Material Progress Spinner: display as overlayAngular 2 Material Progress Spinner:显示为覆盖
【发布时间】:2017-03-22 22:00:09
【问题描述】:

如何将 Angular 2 Material Progress Spinner 显示为当前视图(页面或模式对话框)的某种透明覆盖?

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    我的灵感来自:Overriding Angular Material Size and Styling of md-dialog-container

    我是这样解决的:

    创建一个新组件

    新建一个组件 ProgressSpinnerDialogComponent

    progress-spinner-dialog.component.html的内容:

    <mat-spinner></mat-spinner>
    

    progress-spinner-dialog.component.ts的内容:

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-progress-spinner-dialog',
      templateUrl: './progress-spinner-dialog.component.html',
      styleUrls: ['./progress-spinner-dialog.component.css']
    })
    export class ProgressSpinnerDialogComponent implements OnInit {
    
      constructor() { }
    
      ngOnInit() {
      }
    
    }
    

    添加样式

    在styles.css中添加:

    .transparent .mat-dialog-container {
        box-shadow: none;
        background: rgba(0, 0, 0, 0.0);
    }
    

    使用组件

    这里是进度微调器的示例用法:

    import { Component, OnInit } from '@angular/core';
    import { MatDialog, MatDialogRef } from "@angular/material/dialog";
    import { Observable } from "rxjs";
    import { ProgressSpinnerDialogComponent } from "/path/to/progress-spinner-dialog.component";
    
    @Component({
      selector: 'app-use-progress-spinner-component',
      templateUrl: './use-progress-spinner-component.html',
      styleUrls: ['./use-progress-spinner-component.css']
    })
    export class UseProgressSpinnerComponent implements OnInit {
    
      constructor(
        private dialog: MatDialog
      ) {
        let observable = new Observable(this.myObservable);
        this.showProgressSpinnerUntilExecuted(observable);
      }
    
      ngOnInit() {
      }
    
      myObservable(observer) {
        setTimeout(() => {
          observer.next("done waiting for 5 sec");
          observer.complete();
        }, 5000);
      }
    
      showProgressSpinnerUntilExecuted(observable: Observable<Object>) {
        let dialogRef: MatDialogRef<ProgressSpinnerDialogComponent> = this.dialog.open(ProgressSpinnerDialogComponent, {
          panelClass: 'transparent',
          disableClose: true
        });
        let subscription = observable.subscribe(
          (response: any) => {
            subscription.unsubscribe();
            //handle response
            console.log(response);
            dialogRef.close();
          },
          (error) => {
            subscription.unsubscribe();
            //handle error
            dialogRef.close();
          }
        );
      }
    }
    

    将其添加到 app.module

     declarations: [...,ProgressSpinnerDialogComponent,...],
     entryComponents: [ProgressSpinnerDialogComponent],
    

    【讨论】:

    • 最佳答案就在这里。您可能还想添加选项“disableClose: true”,以便用户无法自行退出
    • @Sharpiro 是的,你是对的,我会更新答案
    • 非常好的解决方案,谢谢! @toongeorges 通过微调器的变化我也会添加“disableClose:true”选项。
    【解决方案2】:

    使用下面的代码来实现不透明:

    HTML

    <div style="height: 800px" [class.hide]="show">
       <button class="btn btn-success" (click)="showSpinner()">Show spinner</button>
    </div>
    <app-spinner [show]="show" [size]="150"></app-spinner>
    

    组件

    import { Component, Input } from '@angular/core';
    
    @Component({
        selector: 'app-spinner',
        template: `
          <i aria-hidden="true" class="fa fa-spinner fa-spin" [style.font-size.px]="size" *ngIf="show"></i>
        `
    })
    export class SpinnerComponent {
      @Input() size = 50;
      @Input() show = false;
      
      showSpinner() {
        this.show = true;
      }
    }
    

    CSS

    .hide {
      opacity: 0;
    }
    

    LIVE DEMO

    【讨论】:

    • 非常感谢阿拉文德!我会尽快测试它!
    • 有什么办法可以整理代码格式,可惜我看不懂代码
    • 我不明白为什么组件和HTML中有模板
    • @AlexanderMills 模板是spinner 组件的HTML; “HTML”是包含spinner 组件的内容。
    【解决方案3】:

    基于有点不同的做法:两个组件,第一个是打开对话框,第二个是对话框。在要显示微调器的组件中添加:

    &lt;app-dialog-spinner *ngIf="progressSpinner"&gt;&lt;/app-dialog-spinner&gt;

    并在您的逻辑中控制 *ngIf。以上是您调用微调器所需的全部内容,以便组件保持整洁。

    对话框微调器组件:

    import { Component, OnInit, OnDestroy } from '@angular/core';
    import { MatDialog, MatDialogRef } from '@angular/material';
    
    // Requires a transparent css (panel)id in a parent stylesheet e.g.:
    // #DialogSpinnerComponent {
    //   box-shadow: none !important;
    //   background: transparent !important;
    // }
    
    @Component({
      selector: 'app-do-not-use',
      template: `<mat-spinner></mat-spinner>`,
      styles: []
    })
    export class DialogSpinnerDialogComponent { }
    
    @Component({
      selector: 'app-dialog-spinner',
      template: ``,
      styles: []
    })
    export class DialogSpinnerComponent implements OnInit, OnDestroy {
    
      private dialog: MatDialogRef<DialogSpinnerDialogComponent>;
    
      constructor(
        private matDialog: MatDialog,
      ) { }
    
      ngOnInit() {
        setTimeout(() => {
          this.dialog = this.matDialog.open(DialogSpinnerDialogComponent, { id: 'DialogSpinnerComponent', disableClose: true });
        });
      }
      ngOnDestroy() {
        setTimeout(() => {
          this.dialog.close();
        });
      }
    
    }
    

    在你的模块中声明组件,当然在你的 entryComponents 中注册DialogSpinnerDialogComponent。将 css 属性添加到父样式表。这可能可以改进,但我有点时间紧迫。

    【讨论】:

    • 非常感谢 Pimmoz!
    猜你喜欢
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多