【问题标题】:set ngModel value to null after changing selected option更改所选选项后将 ngModel 值设置为 null
【发布时间】:2019-11-01 12:32:02
【问题描述】:

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

组件.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】:

    所以最初你的表单必须是这样的:

    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>
    

    那么你必须有 on change 功能:

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

    如果你想让 value 为 null,你可以使用:

    this.createForm.patchValue({
      firstInput:null,
      secondInput:null,
      thirdInput:null,
    })
    

    【讨论】:

    • 感谢您的评论!!我应该在哪里调用 OnChange() 函数?
    • 在 ngOnInit 函数中
    猜你喜欢
    • 2014-09-12
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2019-09-05
    • 2017-07-02
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多