首页 > 解决方案 > 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。

标签: angularobservableangular2-observables

解决方案


推荐阅读