[更改所选选项后将ngModel值设置为null

问题描述 投票:0回答:1

i有一个包含选择选项的表格,而div部分取决于所选选项。在选择选项中,如果我选择1作为示例,则将显示一个输入

component.ts

types : any[] = [
{ value : '1'},
{ value : '2'},
{ value : '3'}
];

createForm = this.fb.group({
firstInput:['',Validators.required],
secondInput:['',Validators.required],
thirdInput:['',Validators.required],
});

component.html

<select class="form-control" [value]="selected"[(ngModel)]="selectedValue">
<option *ngFor="let x of types">{{x.value}}</option>
</select>

<div *ngIf="selectedValue == '1'">
<label for="1" class="control-label">1</label>
<input id="1" class="form-control" type="text" formControlName="firstInput">
</div>

<div *ngIf="selectedValue == '2'">
<label for="2" class="control-label">2</label>
<input id="2" class="form-control" type="text" formControlName="secondInput">
</div>

<div *ngIf="selectedValue == '3'">
<label for="3" class="control-label">3</label>
<input id="3" class="form-control" type="text" formControlName="thirdInput">
</div>

所有字段都是必填字段,我的问题是:当我选择“ 1”作为示例而不填写“ 1”的输入时,然后我更改为第二个选择“ 2”并填写它,我无法提交该表格,因为尽管fielControllName“ firstInput”是不可见的,但它还是空的,因此每次更改我都需要清除ngModel的选定值(按照我的想法)。

angular-reactive-forms angular8 requiredfieldvalidator ngmodel
1个回答
0
投票

因此,最初,您的表单必须类似于:

createForm = this.fb.group({
  selectedValue:[null,Validators.required],
  firstInput:[null],
  secondInput:[null],
  thirdInput:[null],
});

您的HTML代码:

 <form  [formGroup]="createForm">
    <select class="form-control" formControlName="selectedValue">
     <option *ngFor="let x of types" value="{{x.value}}">{{x.value}}</option>
    </select>

    <div *ngIf="createForm.value.selectedValue== '1'">
     <label for="1" class="control-label">1</label>
     <input id="1" class="form-control" type="text" formControlName="firstInput">
    </div>

    <div *ngIf="createForm.value.selectedValue== '2'">
     <label for="2" class="control-label">2</label>
     <input id="2" class="form-control" type="text" formControlName="secondInput">
    </div>

    <div *ngIf="createForm.value.selectedValue== '3'">
     <label for="3" class="control-label">3</label>
     <input id="3" class="form-control" type="text" formControlName="thirdInput">
    </div>
</form>

然后您必须具有更改功能:

    OnChange() {
        this.createForm.get('selectedValue').valueChanges.subscribe(
          val => {
            if (val==1) {
              this.createForm.get('firstInput').setValidators([Validators.required]);
              this.createForm.controls['firstInput'].updateValueAndValidity();
              this.createForm.get('secondInput').setValidators([]);
              this.createForm.controls['secondInput'].updateValueAndValidity();
              this.createForm.get('thirdInput').setValidators([]);
              this.createForm.controls['thirdInput'].updateValueAndValidity();
            }
            else if (val==2) {
              this.createForm.get('firstInput').setValidators([]);
              this.createForm.controls['firstInput'].updateValueAndValidity();
              this.createForm.get('secondInput').setValidators([Validators.required]);
              this.createForm.controls['secondInput'].updateValueAndValidity();
              this.createForm.get('thirdInput').setValidators([]);
              this.createForm.controls['thirdInput'].updateValueAndValidity();
            }
           else if (val==3) {
              this.createForm.get('firstInput').setValidators([]);
              this.createForm.controls['firstInput'].updateValueAndValidity();
              this.createForm.get('secondInput').setValidators([]);
              this.createForm.controls['secondInput'].updateValueAndValidity();
              this.createForm.get('thirdInput').setValidators([Validators.required]);
              this.createForm.controls['thirdInput'].updateValueAndValidity();
            }
          }
        )
      }
© www.soinside.com 2019 - 2024. All rights reserved.