全日历中使用的垫对话框打开两次

时间:2018-08-12 15:46:15

标签: angular dialog fullcalendar angular-material angular6

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

当我单击一个事件时,将显示以下对话框,没有任何信息。

Dialog with no info

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

Dialog with full info

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

感谢您的帮助!

我的主要成分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>

2 个答案:

答案 0 :(得分:5)

我遇到了与此非常相似的问题。我正在网上搜索并尝试查看是否有任何可修复的线索,因为它似乎不像您想出了答案。

我能够解决问题,但是我不确定是什么原因。我所做的是一个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: {}
 });
 ...
});`

答案 1 :(得分:0)

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

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();
  }