首页 > 解决方案 > 如何获取有关输入表单值 Angular 的旧数据

问题描述

我在 Angular 中使用此表单更新数据。单击重置按钮时,我想获取旧输入的值。你们能帮帮我吗?

<form #myform="ngForm" (ngSubmit) = "onSubmit(myform)" class="form form-register">
  <mat-form-field fxLayout="row" appearance="outline" >
  <label for="name">Name: </label>
   <input name="name" id="name" type="text" [(ngModel)]="list['name']" required>
  </mat-form-field>

  <mat-form-field fxLayout="row" appearance="outline" >
  <label for="Location">Location: </label>
  <input name="location" id="location" type="text"  [(ngModel)]="list['location']" required>
  </mat-form-field>
  <input type="hidden" #charactername="ngModel" [(ngModel)]="list['id']" name="id">


 <div>
  <input type="reset" value="Reset" (click)="clearForm(myform)"> 
  <input type="submit" value="Register">
 </div>
  
</form>

标签: angular

解决方案


在创建“列表”的同时,将其值复制到另一个变量。然后在 clearForm() 中,将值复制回“列表”。您甚至可以删除参数“myForm”,因为它包含的值对您没有帮助。


推荐阅读