如何关闭“角材料”对话框?

时间:2020-06-12 08:12:18

标签: jquery angular8 mouseover mat-dialog

我正在使用Angular8。我正在读取svg文件,并发现样式为stroke:none的元素。然后,只要有人悬停该元素,就打开一个对话框。对话框正在打开,但是当我单击外部或关闭按钮时它没有关闭。

我尝试了与按钮id =“ btn”相同的对话框,它已成功关闭。

没有错误。

main.component.html

<object id="svg-object" data="assets/svg/xxx.svg" type="image/svg+xml"></object>

<button mat-button id="btn">Launch dialog</button>

main.component.ts

ngOnInit() {
    this.myfunction();

    $('#btn').mouseover(() => {
      this.openDialog();
    });
}

openDialog() {
    const dialogRef = this.dialog.open(DialogBoxComponent, {
      height: '100px',
      width: '450px',
    });
  }

myfunction() {
    const component = this;
    const mySVG: any = document.getElementById('svg-object');
    mySVG.addEventListener('load',() => {
      svgDoc = mySVG.contentDocument;

      $(svgDoc).find('path').css('fill','fill-opacity','fill-rule','stroke').each(function() {
        const style = $(this).attr('style');

        if($(this).attr('style').includes('stroke:none')) {
          $(this).mouseover(() => {
               component.openDialog();
          });
        }
      });
    }, false);
}

DialogBoxComponent.ts

  constructor(public dialogRef: MatDialogRef<MainComponent>) {
  }

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

DialogBoxComponent.html

<h3 mat-dialog-title>TOPIC</h3>
<div class="container">
  <div class="device-config-main-container d-flex flex-column">
  </div>
  <div mat-dialog-actions>
    <button mat-raised-button matDialogClose (click)="onNoClick()">Close</button>
  </div>
</div>

以下按钮悬停对话框关闭成功运行:

$('#btn').mouseover(() => {
  this.openDialog();
});

2 个答案:

答案 0 :(得分:0)

更改

constructor(public dialogRef: MatDialogRef<MainComponent>)

constructor(public dialogRef: MatDialogRef<DialogBoxComponent>)

在DialogBoxComponent中,考虑以角度方式而不是jquery来实现。

<button mat-button (mouseenter)="mouseEnter() ">Launch dialog</button>

mouseEnter() {
    this.dialogRef = this.dialog.open(DialogBoxComponent, {
       height: '100px',
       width: '450px',
    });
}

,在某些情况下,如果您想获取某些ui元素的参考,请考虑使用ViewChild

答案 1 :(得分:0)

您可以在打开对话框的同时执行此操作,您需要再传递一个参数disableClose: false

openDialog() {
    const dialogRef = this.dialog.open(DialogBoxComponent, {
      height: '100px',
      width: '450px',
      disableClose: false
    });
}

,如果要手动关闭

closeDialog() {
    this.dialog.closeAll();
}