繁体   English   中英

清除Angular中的表单值

[英]Clear a form value in Angular

我有一个包含选择框的Angular窗体。 选择框具有一个onChange事件,该事件应触发一种方法来清除也在表单中找到的输入字段,但是似乎没有任何作用。

HTML

<div class="ui-g-12 ui-md-12">
    <div class="ui-g-6 ui-sm-12">
        <div class="input-container">
            <label for="identityTypeId">Identity document type*</label>
            <p-dropdown [options]="identityDocTypeArr"(onChange)="clearIDNumberField()" formControlName="identityTypeId" id="identityTypeId" placeholder="Select"></p-dropdown>
        </div>
    </div>
    <div class="ui-g-6 ui-sm-12">
        <div class="input-container">
            <label for="identityValue">Identity number*</label>
            <input id="identityValue" type="text" formControlName="identityValue" size="50" placeholder="0000000000000" (blur)="validateSouthAfricanID()">
        </div>
    </div>
</div>

TS

clearIDNumberField() {
    this.offerFormGroup.get("identityValue").reset(); 
}

我的表单名称是[formGroup] =“ offerFormGroup”

您可以使用setValue设置/清除输入值

this.offerFormGroup.controls['identityTypeId'].setValue("");

您好skyDev看起来像您的代码

 clearIDNumberField() {
    this.offerFormGroup.get("identityValue").reset(); 
  }

无法正常工作,因为您引用了formControl的正确名称,如果我正确的话,应该使用identityTypeId 因此,错误的是实际上引用了控件名称。 据说代码应该

  clearIDNumberField() {
        this.offerFormGroup.get("identityTypeId").reset(); 
      }

代替

clearIDNumberField() {
    this.offerFormGroup.get("identityValue").reset(); 
  }

如果您也张贴了formGroup变量的内容,可能会很有帮助,但是尝试所有上述更改。

尝试这个,

   clearIDNumberField() {
     this.offerFormGroup.reset(); 
   }

您可以清除表单值,在这种情况下,可以清除构成表单的所有控件:

clearIDNumberField() {
     this.offerFormGroup.reset(); 
   }

这会将表格重置为正常状态。

您可以尝试以下功能

clearIDNumberField() {
 this.offerFormGroup.identityValue = ''; 
}

您可以使用以下方法重置:

this.offerFormGroup.get("identityValue").setValue('')

this.offerFormGroup.controls["identityValue"].setValue('');

您应该使用setvalue('')方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM