angular - FormGroupDirective.resetForm() 调用 Observable 事件
问题描述
我在静止角形时遇到问题。我的打字稿文件中有以下代码 -
@ViewChild('editNgForm', { static: true }) editNgForm: FormGroupDirective;
officeComposeForm: FormGroup;
ngOnInit(): void {
const officeTypeChanges: Observable<RefData> = this.officeComposeForm.get('officeType').valueChanges;
officeTypeChanges.pipe(takeUntil(this.ngUnsubscribe)).subscribe((officeType: RefData) => {
const selectedEmailTypeCode = this.officeService.getEmailTemplateTypeForofficeTemplate(officeType.code);
this.filteredEmailTemplates = this.emailTemplates.filter(
emailTemplate => emailTemplate.code === selectedEmailTypeCode
);
this.emailTemplateContent = '';
emailContentControl.setValue('');
emailSubjectControl.setValue('');
});
}
rebuildForm() {
if (this.editNgForm) {
this.editNgForm.resetForm();
}
this.filteredEmailTemplates = this.emailTemplates;
this.officeComposeForm.reset();
this.createForm();
}
以及模板中的以下代码 -
<form name="editForm" role="form" [formGroup]="officeComposeForm" #editNgForm="ngForm"
autocomplete="off">
...
<div fxFlex="30" >
<mat-form-field fxFlex>
<mat-label><span jhiTranslate="office.officeType">Office Type</span></mat-label>
<mat-select
id="field_officeType"
formControlName="officeType"
class="search-select"
required
>
<mat-option
[value]="officeType"
*ngFor="let officeType of officeTypes; trackBy: trackById"
>{{ officeType.description }}</mat-option
>
</mat-select>
<mat-error *ngIf="formErrors['officeType']">{{ formErrors['officeType'] | translate }}</mat-error>
</mat-form-field>
</div>
...
</form>
我在我的表单重置点击时调用rebuildForm()函数。相同的代码也以其他形式出现,但它不会调用我们在ngOnInIt中设置的 observable 。我试图将'eventEmiter'作为错误参数作为重置表单的一部分传递,但它会传播事件并且我在可观察订阅中获得空值。
this.editNgForm.resetForm({emitEvent:false});
this.officeComposeForm.reset({emitEvent:false});
我期待在表单重置期间不调用任何 observable。
解决方案
推荐阅读
- sql - 如何忽略 IF EXISTS 块中的 GO 语句?
- xamarin - 如何在 XamarinForms.Microchart 中选择数据点
- php - 如何为 shell_exec() 中的命令命名?
- java - 如何在eclipse中获取活动java项目的名称
- reactjs - 在依赖项中调度。哪里是真的?我们应该或不应该在反应钩子中添加调度吗?
- flutter - 如何将 BackdropFilter 设置为 Appbar 和 BottomNavigationBar?
- html - 列表标题的固定位置,在容器内
- javascript - Bootstrap 4 toogle 没有隐藏下一个 div
- r - 改变长度不同的列
- python - 将 url 字符串从“+”解析为“%2B”