提交成功时如何重置字段



我的问题是成功保存数据后,表单中的一个字段未清除。我从另一个组件调用此字段

我用this.addDiagnosisForm.reset();重置了表单,并试图用(<HTMLInputElement>document.getElementById('diagnosisField')).remove;清除问题字段。但事实并非如此,因为该领域并未完全清除

    <div class="form-row">
            <label for="problemField" class="col-sm-2 col-form-label">problemField<span class="required-span">*</span></label>
            <div class="col-sm-8 d-flex">
                <app-dictionary-entry-search name="problemField" [dictCode]="'problemField'" class="w-100 mr-1"
                (onSetDictionaryEntry)="setDictionaryEntry($event)"  required></app-dictionary-entry-search>
            </div>
      </div>
    <div class="form-row">
            <label for="successField"  class="col-sm-2 col-form-label">successField<span class="required-span">*</span></label>
            <div class="col-sm-8">
              <select class="form-control custom-select  validation-field" name="successField"  [(ngModel)]="successField" required>
                <option *ngFor="let item of successField" [ngValue]="item.code">{{successField}}</option>
              </select>
      </div>
    </div>

<input class="form-control" id="diagnosisField" 
       [class.is-invalid]="searchFailed" [disabled]="disabled"
       (selectItem)="getItemValue($event)" [ngbTypeahead]="search"
       [editable]="false" [resultTemplate]="rt"
       [inputFormatter]="formatter"
       [placeholder]="disabled ? '' : 'testMessage'"
       [(ngModel)]="value"/> 

这个答案是基于AngularJS的,但我认为它可能会对你有所帮助。

首先尝试像这样分配您的ngModel:在表单中声明一个公共范围变量:

$scope.formName = {};

然后将 ngModel 分配给像这样的文件:用户名 :

$scope.formName.username

密码:

$scope.formName.password

继续以这种方式前进。因此,当您需要清除所有表单元素时,请再次输入此代码:

$scope.formName = {};

这将重新分配/清空变量。如果您有任何疑问,或者它解决了您的问题,请选择此作为您的答案并投票。

最新更新