I am successfully using canDeactivate to provide a warning message if a user navigates from a page with a dirty form:
The code that I am using is here:
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
This is where I got the code:
https://scotch.io/courses/routing-angular-2-applications/candeactivate
However I would like to use a angular2 Dialog. Here is my code:
//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
}
What happens when i navigate away is this:
1) I go to the page where a navigate
2) the Dialog then show..
How to have the Dialog block like the below code?
window.confirm('Discard changes?')