[英]Clear a form value in Angular
I have an Angular form that contains a select box. 我有一个包含选择框的Angular窗体。 The select box has an onChange event which should trigger a method to clear an input field also found in the form however nothing seems to be working.
选择框具有一个onChange事件,该事件应触发一种方法来清除也在表单中找到的输入字段,但是似乎没有任何作用。
html 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 TS
clearIDNumberField() {
this.offerFormGroup.get("identityValue").reset();
}
My forms name is [formGroup]="offerFormGroup" 我的表单名称是[formGroup] =“ offerFormGroup”
您可以使用setValue设置/清除输入值
this.offerFormGroup.controls['identityTypeId'].setValue("");
Hello skyDev it looks like your code 您好skyDev看起来像您的代码
clearIDNumberField() {
this.offerFormGroup.get("identityValue").reset();
}
is not working because your reference the correct name of the formControl which if I am correct should identityTypeId
. 无法正常工作,因为您引用了formControl的正确名称,如果我正确的话,应该使用
identityTypeId
。 Hence what is wrong is actualy the control name been referenced. 因此,错误的是实际上引用了控件名称。 That been said the code should
据说代码应该
clearIDNumberField() {
this.offerFormGroup.get("identityTypeId").reset();
}
instead of 代替
clearIDNumberField() {
this.offerFormGroup.get("identityValue").reset();
}
It might have been of good help if you had posted the contents of your formGroup variable too, but all the try the above changes. 如果您也张贴了formGroup变量的内容,可能会很有帮助,但是尝试所有上述更改。
Try This, 尝试这个,
clearIDNumberField() {
this.offerFormGroup.reset();
}
You can clear the forms value, in this case all the controls that makes up the form: 您可以清除表单值,在这种情况下,可以清除构成表单的所有控件:
clearIDNumberField() {
this.offerFormGroup.reset();
}
This will reset the form to normal. 这会将表格重置为正常状态。
You can Try below function 您可以尝试以下功能
clearIDNumberField() {
this.offerFormGroup.identityValue = '';
}
You can reset with: 您可以使用以下方法重置:
this.offerFormGroup.get("identityValue").setValue('')
and 和
this.offerFormGroup.controls["identityValue"].setValue('');
您应该使用setvalue('')
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.