执行订阅部分之前显示的表单组错误

时间:2018-05-21 06:32:50

标签: angular

我正在创建一个表单构建器(createForm()),但在更新期间,执行在订阅部分工作之前完成。所以它会抛出错误消息

  

formGroup需要一个FormGroup实例。请通过一个。

但表格附带了预期的数据。

if (output.action === 'create') {
  this.createForm();
} else if (output.action === 'update') {
  /* Get physician data from service */
  const physicianData = this.physervice.getPhysician(id);
  /* Subscribe to the service */
  physicianData.subscribe( res => {
    this.physicianData = res.body;
    this.createForm();
  });
}

创建表单用于创建表单构建器。

createForm() {
    this.rules = new Rules();
    this.physicianForm = this.fb.group({
    name: new FormControl(((this.physicianData) ? 
        this.physicianData.name : ''), this.rules.name),
......
......
}
  

模板代码:

<form [formGroup]="physicianForm">
<div class="form-group">
  <label class="center-block">Name
    <input autocomplete="off" id="name" class="form-control" formControlName="name" value="" required>
  </label>

  <div *ngIf="name.invalid && (name.dirty || name.touched)" class="alert alert-danger">
    <div *ngIf="name?.errors['required']">
      Name is required.
    </div>
    <div *ngIf="name?.errors['minlength']">
      Name must be at least 4 characters long.
    </div>
    <div *ngIf="name?.errors['pattern']">
      Name must be alphabetic characters it should not include numbers or other characters
    </div>
  </div>
.............
.............
</div>
  

解决方案1:如果条件有效,我尝试调用createForm()。但我讨厌两次打电话。

还有其他选择吗?

提前致谢。

0 个答案:

没有答案
相关问题