简体   繁体   English

使用 candeactive 实现角材料对话框

[英]Implementing angular material dialog box with candeactive

problem faced when implement angular 4 material dialog box in candeactive在candeactive中实现angular 4 material对话框时遇到的问题

Whenever i navigate from one page to another if any changes in form field, i need to indicate "There is unsaved data.Do you want to close".每当我从一个页面导航到另一个页面时,如果表单字段有任何更改,我需要指出“有未保存的数据。是否要关闭”。 This should come up with confirmation window with OK and cancel button.这应该会出现带有确定和取消按钮的确认窗口。

On click of OK, it should navigate to other page else it must be in same page.单击确定后,它应该导航到其他页面,否则它必须在同一页面中。 I have tried with candeactive, it is perfectly working when I use window.confirm我尝试过使用 candeactive,当我使用 window.confirm 时它完美地工作

 return window.confirm('There is unsaved data.Do you want to close?').

But My requirement is to implement the confirm window using Angular material dialog box.但我的要求是使用 Angular 材质对话框实现确认窗口。 https://material.angular.io/components/dialog/overview https://material.angular.io/components/dialog/overview

The problem is before retrieved the result from afterClosd.问题是在从 afterClosd 检索结果之前。 it returned false.So whenever i clicks OK or Cancel in dialog box, it didn't move to another screen.它返回false.So 每当我在对话框中单击“确定”或“取消”时,它都不会移动到另一个屏幕。

openDialog(): boolean {
    let dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
      width: '500px'

    });

    dialogRef.afterClosed().subscribe(result => {           
      if (result){
         return true;
      }else{
        return false;
      } 
    });
   return false;

  }

I assume you didn't see this part in the documentation ?我假设您没有在文档中看到这部分?

<button mat-button mat-dialog-close>Cancel</button>
<button mat-button [mat-dialog-close]="true">Leave</button>

Also, you should remove your last return false statement :此外,您应该删除最后一个return false语句:

dialogRef.afterClosed().subscribe(result => {           
  if (result){
     return true;
  }else{
    return false;
  } 
});
// return false;

The modal is asynchronous as you can see with subscribe : this means that you are waiting for an user intercation before returning something.正如您在subscribe看到的那样,模态是异步的:这意味着您在返回某些内容之前正在等待用户交互。

In your case, you return false before the user action.在您的情况下,您在用户操作之前返回 false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM