【问题标题】:Angular 2 Window.onbeforeunload Show primeng ConfirmDialog not working while closing windowAngular 2 Window.onbeforeunload Show primeng ConfirmDialog 在关闭窗口时不起作用
【发布时间】:2017-12-14 22:49:01
【问题描述】:

请帮助如何在关闭选项卡/窗口或离开页面时保留对话框

export class AppComponent{constructor(private confirmationService:ConfirmationService){}@HostListener('window:beforeunload', ['$event'])    public onWindowScroll( $event:any){      console.log('scrolled');
      return new Promise((observer) => {this.confirmationService.confirm({
           message:"All the information would be lost, are you sure you want to move out of the screen?", accept: () => { $event.returnValue ="";
              observer(true);
            },
            reject: () => {

                observer(false);
            }
        });
    });
    }
}

【问题讨论】:

    标签: angular angular2-routing primeng


    【解决方案1】:
    ngOnInit() {       
            const unloadEvent = (e) => {
              if (**YOUR CHECK FLAG**) {
                const confirmationMessage =
                  'Warning: Leaving this page will result in any unsaved data being lost. Are you sure you wish to continue?';
                (e || window.event).returnValue = confirmationMessage; // Gecko + IE
                return confirmationMessage; // Webkit, Safari, Chrome etc.
              }
            };
            window.addEventListener('beforeunload', unloadEvent);
            window.onunload = () => {
             // Post Unload operations
                }
              });
            };
          }
    

    【讨论】:

    • 但这是默认的确认窗口。我需要的是像模态对话框一样的 ConfirmDialog 用户必须说是或否,而不是留下或留下消息。
    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 2015-01-16
    • 2017-06-20
    • 2019-05-04
    • 2018-06-06
    • 2016-12-24
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多