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

时间:2019-10-31 14:21:37

标签: angular-reactive-forms angular8 requiredfieldvalidator ngmodel

我有一个包含选择选项的表格,而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的选定值(按照我的想法)。

1 个答案:

答案 0 :(得分:1)

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

foreach (DataGridCell cell in AppraiseeDataGrid.SelectedCells)//It says it cant convert type datagridcellinfo to datagricell

您的HTML代码:

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

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

 <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();
            }
          }
        )
      }