【问题标题】:mat dialog that is used in fullcalendar opens twice在 fullcalendar 中使用的 mat 对话框打开两次
【发布时间】:2019-01-19 11:28:25
【问题描述】:

我正在使用在 ap-fullcalendar 上单击事件时执行的 mat 对话框。我正在使用 Angular6。

当我单击一个事件时,会出现以下对话框,没有任何信息。

按下“购买”按钮后,它会将我带到实际的对话框。

注意:按钮似乎没有按预期工作,但是,我认为这是我最初的问题。 此外,当我双击我的事件时,我会在对话框打开时得到不同的行为,如第一个屏幕截图所示。当我按下或取消时,它会正常关闭。

感谢任何帮助!

我的主要组件TS:

import { Component, OnInit, ViewChild, Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { CalendarComponent } from 'ap-angular2-fullcalendar/src/calendar/calendar';
import { CalendarService } from '../_services/calendar.service';
import { DialogComponentComponent } from './dialog-component/dialog-component.component';

export interface DialogData {
  animal: string;
  name: string;
}

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent{

  calendarOptions: any;
  displayEvent: any;
  @ViewChild(CalendarComponent) ucCalendar: CalendarComponent;
  animal: string;
  name: string;
  subjectFilter: string;

  constructor(protected calendarService: CalendarService, private dialog: MatDialog) { }

  ngOnInit(){
    this.calendarService.getEvents(this.subjectFilter).subscribe(data => {
      console.log(data);
      this.calendarOptions = {
        editable: true,
        eventLimit: false,
        header: {
          left: 'prev,next today',
          center: 'title',
          right: 'month,agendaWeek,agendaDay,listMonth'
        },
        events: data,
        eventClick: (calEvent, jsEvent, view) => {
          this.openDialog(calEvent);

          console.log('Event: ' + calEvent.title);
          console.log('Coordinates: ' + jsEvent.pageX + ',' + jsEvent.pageY);
          console.log('View: ' + view.name);
        },
      };
    });
  }

  openDialog(calEvent): void {
    console.log("opendialog");
    const dialogRef = this.dialog.open(DialogComponentComponent, {
      data : {
        title: calEvent.title,
        start: calEvent.start,
        end: calEvent.end,
        price: calEvent.price
      }
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
    });
  }
}

我的主要组件 HTML

<div *ngIf="calendarOptions">
  <angular2-fullcalendar #ucCalendar [options]="calendarOptions" (eventDrop)="updateEvent($event.detail)"
              (eventResize)="updateEvent($event.detail)">
  </angular2-fullcalendar>
</div>

我的对话组件TS:

import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';

@Component({
  selector: 'app-dialog-component',
  templateUrl: './dialog-component.component.html',
  styleUrls: ['./dialog-component.component.css']
})
export class DialogComponentComponent {

  constructor(
    public dialogRef: MatDialogRef<DialogComponentComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any
  ) {console.log("constructor");}

  onNoClick(): void {
    this.dialogRef.close();
  }
}

我的对话框组件 HTML:

<h2 mat-dialog-title>{{data.title}}</h2>

<mat-dialog-content>
  <p>Lesson Details:</p>
  <p>Start - {{data.start}}</p>
  <p>End - {{data.end}}</p>
  <p>Price - {{data.price}}</p>
</mat-dialog-content>

<mat-dialog-actions>
  <button mat-raised-button mat-button>Cancel</button>

  <button mat-raised-button (click)="onNoClick()">Buy</button>
</mat-dialog-actions>

【问题讨论】:

  • 这个运气好吗?

标签: angular dialog fullcalendar angular-material angular6


【解决方案1】:

我遇到了与此非常相似的问题。我在网上搜索并试图查看是否有任何解决问题的线索,因为看起来你没有找到答案。

我能够让问题自行解决,但我不确定它到底是做什么的。我所做的是一个npm update,然后删除了node_modules 文件夹并运行了一个新的npm install

接下来,我遇到了一些其他问题,有些人遇到了类似的问题。看起来您可以通过window 调用弹出对话框来轻松解决问题。事实证明,一些问题是出现的对话框发生在 NgZone 之外,所以要修复,import {NgZone} from '@angular/core', 将其包含在您的构造函数 constructor(private dialog: MatDialog, readonly ngZone: NgZone) 中,然后在打开对话框时使用它

this.ngZone.run(() => {
 const ref = this.dialog.open(DialogComponent, {
  options..,
  data: {}
 });
 ...
});`

【讨论】:

  • 同样的事情,当我尝试在全局错误处理程序上打开一个对话框时(提供重新加载窗口和其他选项,以及错误消息和报告发送)。使用ngZone.run() 解决了!
  • 这里也一样。我在一个全局错误处理程序上打开对话框并且得到了各种疯狂的行为。感谢您的修复。
【解决方案2】:

您可以手动触发更改检测。这是更新后的代码。

import {ChangeDetectorRef} from '@angular/core';

constructor(private cdr: ChangeDetectorRef){}

openDialog(calEvent): void {
    console.log("opendialog");
    setTimeout(()=>{
        const dialogRef = this.dialog.open(DialogComponentComponent, {
        data : {
          title: calEvent.title,
          start: calEvent.start,
          end: calEvent.end,
          price: calEvent.price
          }
        });
    }, 0);


    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
    });
    // this.cdr.detectchanges();
  }

【讨论】:

  • 这没有帮助。至此,它们基本上都已加载。检测线发生在...
  • 好的,将打开的块放入 setTimeout({}, 0);更新了代码
  • 嗨,这仍然没有帮助,仍然是相同的行为。也许它不是这个 openDialog 函数的问题,而是对话框组件代码?
猜你喜欢
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
相关资源
最近更新 更多