Angular反应形式针对所有迭代进行验证

时间:2020-06-22 08:25:32

标签: angular typescript

我正在reactive forms内使用*ngFor在我的应用程序中进行验证。但是,当我仅尝试验证一次迭代时,该表单将在所有迭代中都得到验证。

我尝试使用模板驱动的表单,并且运行良好。但是我想使用反应形式来学习它。

下面是我的代码

HTML

<div *ngFor = "let f of flowers">
<div>{{f.type}} </div>
 <div class="form-group col-sm-4">
  <label>Flower License</label>
  <input class="form-control" type="text" formControlName="fLicense" [class.invalid]="!statusForm.controls['fLicense'].valid && statusForm.controls['fLicense'].touched ">
  <div *ngIf="!statusForm.controls['fLicense'].valid && (statusForm.controls['fLicense'].touched || isSubmitted)">
    <div class="invalid-feedback" style="display: block;">Please enter flower License Number</div>
  </div>
</div>
</div>

ts

ngOnInit() {
  this.statusForm = this.formBuilder.group({
    fLicense: ['', Validators.required],
  });
}

submitStatus(f) {
  this.isSubmitted = true;
  // stop here if form is invalid
  if (this.statusForm.invalid) {
    return;
  }
}

0 个答案:

没有答案