【发布时间】:2017-03-22 22:00:09
【问题描述】:
如何将 Angular 2 Material Progress Spinner 显示为当前视图(页面或模式对话框)的某种透明覆盖?
【问题讨论】:
如何将 Angular 2 Material Progress Spinner 显示为当前视图(页面或模式对话框)的某种透明覆盖?
【问题讨论】:
我的灵感来自: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],
【讨论】:
使用下面的代码来实现不透明:
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;
}
【讨论】:
spinner 组件的HTML; “HTML”是包含spinner 组件的内容。
基于有点不同的做法:两个组件,第一个是打开对话框,第二个是对话框。在要显示微调器的组件中添加:
<app-dialog-spinner *ngIf="progressSpinner"></app-dialog-spinner>
并在您的逻辑中控制 *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 属性添加到父样式表。这可能可以改进,但我有点时间紧迫。
【讨论】: