简体   繁体   English

致电服务以打开一个对话框,取决于setTimeout

[英]Call Service to open a dialog depend on setTimeout

Hi As I am new to angular 4 I am facing some issue like "Uncaught Error: Can't resolve all parameters for" or Cyclic dependence . 您好,因为我是angular 4的新手,所以我遇到了一些问题,例如“未捕获的错误:无法解决所有参数”或循环依赖。 I have written a service which does a calculation and in setTimeout I am calling to open the MatDialog . 我编写了一个服务进行计算,并在setTimeout中调用了打开MatDialog的服务。 Matdialog has two option 'Yes' or 'no' on click of the 'Yes' doing some service call and doing some calculation and again setting clearTimeout and setting new setTimeout which again open the popup after some time. Matdialog单击“是”时有两个选项“是”或“否”,进行一些服务调用并进行计算,然后再次设置clearTimeout和设置新的setTimeout,这将在一段时间后再次打开弹出窗口。

And I want to also check on each service call and depending on some condition I have again clearTimeout and set a new setTimeout which open the MatDialog . 而且我还想检查每个服务调用,并根据某些条件再次设置clearTimeout并设置一个新的setTimeout来打开MatDialog。

I trying this since long time but would not find a solution . 很久以来我一直尝试这样做,但找不到解决方案。 I want know Where is the right place that I can place my code and How to write service to open the Matdialog . 我想知道我可以在哪里放置我的代码,以及如何编写服务以打开Matdialog。

Written this code in main.components.ts 将此代码写在main.components.ts中

setTimer() {

    this.notifyTime = expiryValue - 120000;
    this.date1 = new Date();
    this.date2 = new Date(this.notifyTime);
    this.diff = this.date2.getTime() - this.date1.getTime();
    let _self = this;
    this.timerVar = setTimeout(function () {
        let dialogRef = _self.dialog.open(DialogModalComponent, {
            data: {
                timer: _self.timerVar,
                va: true
            }
        });
    }, this.diff);
}


clearTimer() { 
    clearTimeout(this.timerVar);
}

The above is a piece of code I am using to setTimeout() and clearTimeout() 上面是我用于setTimeout()和clearTimeout()的一段代码

Written this code in global service where temp points to another to main.component.ts 将此代码写在全局服务中,其中temp指向main.component.ts的另一个

autoLoad() {

    if (this.expiryValue) {
        this.date1 = new Date();
        this.diff = this.expiryValue - this.date1.getTime();
        if (this.diff < 600000 && this.diff > 120000) {
            this.getUpUrl('refresh').then(result => {
                if (result.status == 'success') {
                    this.temp.clearTimer();
                    this.temp.showDialog(result.sessionExpiry);
                } 
            });
        }
    }

And in the dialog.component.ts 并在dialog.component.ts中

 ok() {
    this.dialog.close();
    this.temp.clearTimer();
    this.temp.setTimer();
 }

cancel() {
    this.dialog.close();
}

The above code I am using in the dialog. 我在对话框中使用上面的代码。 temp points to my main.component.ts 临时指向我的main.component.ts

you can use a setTimeout function to open a dialog after some time 您可以在一段时间后使用setTimeout函数打开对话框

this example based on angular material examples 本示例基于角材示例

  constructor(public _dialogService: DialogService) { }

  openDialog(): void {
    this.clicked = true;
    this._dialogService.open({ name: this.name, animal: this.animal }, () => this.clicked = false)
      .then(result => {
        console.log('The dialog was closed');
        this.animal = result;
      })
  }

dialog service 对话服务

@Injectable({
  providedIn:'root'
})
export class DialogService {

  constructor(private dialog: MatDialog) { }

  open(data , onOpenCallBack , timeout:number = 2500) : Promise<any> {
    return new Promise((resolve) => {

    setTimeout(() => {

      const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
        width: '250px',
        data: data
      });

      if (onOpenCallBack) {
        onOpenCallBack()
      }

      dialogRef.afterClosed().subscribe(result => {
        resolve(result)  
      });
    }, timeout)
    })
  }
}

stackblitz demo stackblitz演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM