[英]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.