Close pop up on back button

后端 未结 10 1539
长发绾君心
长发绾君心 2020-12-08 08:15

I want to close pop up on click of back button for mobile. I implemented this using onhashchange:

window.onhashchange = function (event) {

};
10条回答
  •  长情又很酷
    2020-12-08 08:32

    My Solution in Angular

    // push history state when a dialog is opened
    dialogRef.afterOpened.subscribe((ref: MatDialogRef) => {
      window.history.pushState(ref.id, '');
      // pop from history if dialog has not been closed with back button, and gurrent state is still ref.id
      ref.afterClosed().subscribe(() => {
        if (history.state === ref.id) {
          window.history.go(-1);
        }
      });
    });
    

提交回复
热议问题