angular - 如何使用angular2 material Dialog 来block for canDeactivate

标签 angular angular-material angular2-routing

如果用户从带有脏表单的页面导航,我成功地使用 canDeactivate 提供警告消息:

我使用的代码在这里:

    is_submit = false;
   canDeactivate() {
       //https://scotch.io/courses/routing-angular-2-applications/candeactivate
        if (this.myForm.dirty == true && this.is_submit==false){
            return window.confirm('Discard changes?');
        } //end if
        return true
    } // end  canDeactivate

这是我得到代码的地方:

https://scotch.io/courses/routing-angular-2-applications/candeactivate

但是我想使用 angular2 对话框。这是我的代码:

    //ts for the main component

    is_submit = false;
   canDeactivate() {
        if (this.myForm.dirty == true && this.is_submit==false){
            const config = new MdDialogConfig();
            config.disableClose=true;
            let dialogRef = this.dialog.open(DialogCanDeactive,config);
            dialogRef.afterClosed().subscribe(result => {
                if (result=='cancel'){
                    return false;
                } 
                if (result=='save'){
                    return true;
                } 
                if (result=='discard'){
                    return true;
                } 
            }); //end dialogRef
        } //end if
        return true
    }

 ///Code for the dialog

@Component({
  selector: 'can_deactive_dialog',
  template: `
<div>
    <button md-raised-button (click)="dialogRef.close('cancel')">Cancel</button>
    <button md-raised-button (click)="dialogRef.close('save')">Save Changes</button>
    <button md-raised-button (click)="dialogRef.close('discard')">Discard Changes</button>
</div>
`,
})
export class DialogCanDeactive {


  constructor(public dialogRef: MdDialogRef<DialogCanDeactive>) {} //end constructor

}

当我离开时会发生什么:

1) 我转到导航页面

2) 对话框然后显示..

如何让 Dialog block 像下面的代码一样?

    window.confirm('Discard changes?')

最佳答案

canDeactivate 方法也可以返回一个 Promise 或 Observable。您应该返回它并解决 promise 或在可观察到的结果上发出一个值,并得到您想要的结果。

在您的特定示例中,您可以从 afterClosed 方法返回 observable 而不是订阅它,并将其映射到 bool 值:

return dialogRef.afterClosed().map(result => {
                if (result=='cancel'){
                    return false;
                } 
                if (result=='save'){
                    return true;
                } 
                if (result=='discard'){
                    return true;
                } 
            }).first();

我也会从守卫中移出这个逻辑,例如在组件中,然后从那里调用一个方法。

关于angular - 如何使用angular2 material Dialog 来block for canDeactivate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45251348/

相关文章:

node.js - 在 Angular 和 Node.js 项目之间共享代码

javascript - FBQ(Facebook 事件)与 Angular 4

javascript - 如何在 Angular 中获取 clientHeight?

angular-material - 角度 Material ,定义主题颜色时如何使用 dark() 函数?

angular - 在 Angular4 和 Material2 中提交表单

javascript - 类型 'navigate' 上不存在属性 'Router' 。你的意思是 'navigated' 吗?

angular - 错误 : No Component Factory Found. 是否添加到@NgModule.entryComponents?

angular - 如何在 Angular 6 的 Material 日期选择器中编辑日期格式?

Angular:解析失败时显示目标 URL

angular - 进行硬刷新时,路由对我不起作用