gpt4 book ai didi

Angular 在 canDeactivate Guard 服务中使用模态对话框进行未提交的更改(表单脏)

转载 作者:太空狗 更新时间:2023-10-29 17:11:49 28 4
gpt4 key购买 nike

在我的 Angular 4 应用程序中,我有一些带有表单的组件,如下所示:

export class MyComponent implements OnInit, FormComponent {

form: FormGroup;

ngOnInit() {
this.form = new FormGroup({...});
}

他们使用 Guard 服务来防止未提交的更改丢失,因此如果用户尝试在它要求确认之前更改路线:

import { CanDeactivate } from '@angular/router';
import { FormGroup } from '@angular/forms';

export interface FormComponent {
form: FormGroup;
}

export class UnsavedChangesGuardService implements CanDeactivate<FormComponent> {
canDeactivate(component: FormComponent) {
if (component.form.dirty) {
return confirm(
'The form has not been submitted yet, do you really want to leave page?'
);
}

return true;
}
}

这是使用一个简单的 confirm(...) 对话框,它工作得很好。

但是我想用更花哨的模式对话框替换这个简单的对话框,例如使用 ngx-bootstrap Modal .

我怎样才能使用模态来实现相同的结果?

最佳答案

我用 ngx-bootstrap Modals 解决了它和 RxJs Subjects .

首先我创建了一个模态组件:

import { Component } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { BsModalRef } from 'ngx-bootstrap';

@Component({
selector: 'app-confirm-leave',
templateUrl: './confirm-leave.component.html',
styleUrls: ['./confirm-leave.component.scss']
})
export class ConfirmLeaveComponent {

subject: Subject<boolean>;

constructor(public bsModalRef: BsModalRef) { }

action(value: boolean) {
this.bsModalRef.hide();
this.subject.next(value);
this.subject.complete();
}
}

这是模板:

<div class="modal-header modal-block-primary">
<button type="button" class="close" (click)="bsModalRef.hide()">
<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
</button>
<h4 class="modal-title">Are you sure?</h4>
</div>
<div class="modal-body clearfix">
<div class="modal-icon">
<i class="fa fa-question-circle"></i>
</div>
<div class="modal-text">
<p>The form has not been submitted yet, do you really want to leave page?</p>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-default" (click)="action(false)">No</button>
<button class="btn btn-primary right" (click)="action(true)">Yes</button>
</div>

然后我用一个 Subject 修改了我的守卫,现在它看起来像这样:

import { CanDeactivate } from '@angular/router';
import { FormGroup } from '@angular/forms';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { BsModalService } from 'ngx-bootstrap';

import { ConfirmLeaveComponent } from '.....';

export interface FormComponent {
form: FormGroup;
}

@Injectable()
export class UnsavedChangesGuardService implements CanDeactivate<FormComponent> {

constructor(private modalService: BsModalService) {}

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;

return subject.asObservable();
}

return true;
}
}

在 app.module.ts 文件中,转到 @NgModule 部分并将 ConfirmLeaveComponent 组件添加到 entryComponents。

@NgModule({
entryComponents: [
ConfirmLeaveComponent,
]
})

关于Angular 在 canDeactivate Guard 服务中使用模态对话框进行未提交的更改(表单脏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46433195/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com