Hello!
I want to implement your dialog with a the CanDeactive Guard. In order to so I have implemented the CanDeactive Guard
import { CanDeactivate } from '@angular/router';import { Observable } from 'rxjs';import { Injectable } from '@angular/core';export interface CanComponentDeactivate { canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;}@Injectable({ providedIn: 'root',})export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> { canDeactivate(component: CanComponentDeactivate) { return component.canDeactivate && component.canDeactivate(); }}
and extended my component and created the following function
canDeactivate(): Observable<boolean> | Promise<boolean> | boolean { const dialog: DialogRef = this.dialogService.open({ title: 'Please confirm', content: 'Are you sure?', actions: [{ text: 'No' }, { text: 'Yes', primary: true }], width: 450, height: 200, minWidth: 250, }); if (this.isInEditState) { dialog.result.subscribe((result: any) => { if (result.text === 'No') { return false; } else { return true; } }); } else { return true; }}
I get a dialog, but the result will not trigger the CanDeactive Guard (I think it is undefinied). I think he looses the scope because of the subscribe. Could you guys help me?
Best regards
Felix
