如何关闭角形材料对话框?

问题描述 投票:0回答:1

我正在使用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.myfucntion();

    $('#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天。找不到解决方案。感谢您的帮助。

谢谢

jquery angular8 mouseover mat-dialog
1个回答
0
投票

更改

constructor(public dialogRef: MatDialogRef<MainComponent>)

to

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

© www.soinside.com 2019 - 2024. All rights reserved.