Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用服务打开对话框取决于setTimeout_Javascript_Angular - Fatal编程技术网

Javascript 调用服务打开对话框取决于setTimeout

Javascript 调用服务打开对话框取决于setTimeout,javascript,angular,Javascript,Angular,嗨,因为我是angular 4的新手,所以我面临一些问题,比如“未捕获错误:无法解析的所有参数”或循环依赖。我已经编写了一个进行计算的服务,在setTimeout中我调用它来打开MatDialog。Matdialog有两个选项“是”或“否”,单击“是”进行一些服务调用和计算,然后再次设置clearTimeout和设置new setTimeout,一段时间后再次打开弹出窗口 我还想检查每个服务调用,根据某些条件,我再次设置clearTimeout,并设置一个新的setTimeout,打开MatDi

嗨,因为我是angular 4的新手,所以我面临一些问题,比如“未捕获错误:无法解析的所有参数”或循环依赖。我已经编写了一个进行计算的服务,在setTimeout中我调用它来打开MatDialog。Matdialog有两个选项“是”或“否”,单击“是”进行一些服务调用和计算,然后再次设置clearTimeout和设置new setTimeout,一段时间后再次打开弹出窗口

我还想检查每个服务调用,根据某些条件,我再次设置clearTimeout,并设置一个新的setTimeout,打开MatDialog

我尝试了很久,但没有找到解决办法。我想知道哪里是我可以放置代码的正确位置,以及如何编写服务来打开对话框

在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);
}
上面是我用来设置超时()和clearTimeout()的一段代码

在全局服务中编写此代码,其中 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);
                } 
            });
        }
    }
 ok() {
    this.dialog.close();
    this.temp.clearTimer();
    this.temp.setTimer();
 }

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

我在对话框中使用的上述代码。temp指向my main.component.ts

您可以使用设置超时功能在一段时间后打开一个对话框

此示例基于角度材质示例

  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;
      })
  }
对话服务

@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)
    })
  }
}
@可注入({
providedIn:“根”
})
导出类对话框服务{
构造函数(私有对话框:MatDialog){}

打开(数据,onOpenCallBack,超时:number=2500):Promise

你能分享一些代码吗?你不必使用自欺欺人的方法,你可以使用箭头功能,我已经完成了这一部分,但代码的位置是给我一些错误。请检查我在上述问题中提到的错误。我已经更新了我的答案,并创建了一个服务来打开一个对话框。希望这对@Ashoka有所帮助