Angular 2 Material Progress Spinner:显示为叠加层

时间:2017-03-22 22:00:10

标签: angular angular-material

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

3 个答案:

答案 0 :(得分:18)

我的灵感来自: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";
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],

答案 1 :(得分:5)

使用以下代码实现不透明

HTML

<div style="height:800px" [class.ops]="show">
   <h2 [class.opaque]="trans">{{name}}</h2>
   <button class="btn btn-success" (click)="toggleSpinner()"> See spinner </button>
</div>
<spinner [show]="show" [size]="150"> </spinner>

COMPONENT

import { Component, Input } from '@angular/core';

@Component({
    selector: 'spinner',
    template: `
      <div *ngIf="show">
          <span>
           <i class="fa fa-spinner fa-spin" [ngStyle]="{'font-size': size+'px'}" aria-hidden="true"></i>
          </span>
      </div>
    `
})
export class SpinnerComponent {
    @Input() size: number = 50;
    @Input() show: boolean;

    toggleSpinner() {
       this.show = !this.show;
       this.trans=!this.trans;
    }

}

CSS

.ops {
  opacity :0;
}

LIVE DEMO

答案 2 :(得分:1)

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

<app-dialog-spinner *ngIf="progressSpinner"></app-dialog-spinner>

并控制逻辑中的* ngIf。上面就是你需要调用微调器所以组件保持良好和干净。

Dialog微调器组件:

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属性添加到父样式表。这可能会有所改善,但我有点紧张。