Angular use modal dialog in canDeactivate Guard service for unsubmitted changes (Form dirty)

前端 未结 7 2518
北荒
北荒 2020-12-05 19:35

In my Angular 4 application I have some components with a form, like this:

export class MyComponent implements OnInit, FormComponent {

  form: FormGroup;

          


        
相关标签:
7条回答
  • 2020-12-05 20:07

    Just expanding on the additional info provided by mitschmidt regarding click outside / escape button, this canDeactivate method works with Francesco Borzi's code. I just add the subscribe to onHide() inline in the function:

    canDeactivate(component: FormComponent) {
            if (component.form.dirty) {
                const subject = new Subject<boolean>();
    
                const modal = this.modalService.show(ConfirmLeaveComponent, { 'class': 'modal-dialog-primary' });
                modal.content.subject = subject;
    
                this.modalService.onHide.subscribe(hide => {
                    subject.next(false);
                    return subject.asObservable();
                });
    
                return subject.asObservable();
            }
    
            return true;
        }
    
    0 讨论(0)
提交回复
热议问题