Thank you for spending time here.
So Im having little problem with canDeactivate. I made a page If I click go-back button, there will be a modal show up, And user can choose either escape that page or not. Which is works fine If I click only once. But here is the problem. Lets say I was located at [ page A -> page B -> page C ] and on page C, I put canDeactivate. So If I click Ok on Modal when I click go-back, It moves to page B well. But If I click Cancel on Modal first and click go-back again, When I click Ok, It moves to page A. Certainly Whenever I call canDeactivate, It acts like It already moved no matter what I click on modal.
So All I wanna do is no matter how many times I call canDeactivate, When I click Ok on modal, I want to move to Page B. Hope y'all have solution for me.
canDeactivate.file
export class DeactiveGuard implements CanDeactivate<GrammarComponent> {
constructor(private router: Router) {}
canDeactivate(component: GrammarComponent) {
return component.canDeactivate ? component.canDeactivate() : true
}
}
page C.file
public canDeactivate() {
if (!this.exceptCanDeactivate) {
return from(
this.modalService.open(this.exitFromExamMd, {
centered: true,
scrollable: true,
windowClass: 'final-confirm',
}).result,
).pipe(map((result) => result === 'yes'))
} else {
return true
}
}
According to Angular docs, the
canDeactivate
guard can return, among other things, anUrlTree
, anObservable<UrlTree>
and even aPromise<UrlTree>
. AUrlTree
is a way to navigate to wherever you want. As you're specifying the destiny, no matter where you click to go away, you'll always be redirected to the same destiny.This is what you can do: in your guard, return an observable that, internally, will emit just when you close your dialog (in this example I supposed an asynchronous confirmation dialog API, as we have in @angular/material dialog):
In your case, because you're using NgbModal: