I want to replace classic confirm with Alertify confirm in canDeactivate. I tried to implement the following codes but it does not return True when click on OK. Can anyone advise on this?
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { SignupComponent } from 'src/app/signup/signup.component';
import { AlertifyService } from 'src/app/_helpers/alertify.service';
@Injectable()
export class SignUpPUS implements CanDeactivate <SignupComponent> {
constructor(private alertifyService: AlertifyService) {}
canDeactivate(component: SignupComponent) {
if (component.signUpForm.dirty) {
this.alertifyService.confirm('Leave Page', 'Are you sure you want to continue? Any unsaved changes will be lost', () => {
return true;
});
}
return false;
}
}
As
confirmperforms an async operation, you need to use an async solution such asObservable,Promiseorasync/await.You can use
Observableas follows:Edit Note: Note that I added second callback for
confirmto make sure to return false when user cancels confirmation.