最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to store reactive form data to localstorage? - Stack Overflow

programmeradmin1浏览0评论

I want to store reactive forms data to local storage and access that data to another page.

<form [formGroup]="testform" >
      firstname:
          <input type="text" formControlName="fname"><br>
      lastname:
          <input type="text" formControlName="lname">
          <button type="submit" (click)="Submit(testform)">Submit</button>
</form>

I hope anyone knows the issue. Thank you.

I want to store reactive forms data to local storage and access that data to another page.

<form [formGroup]="testform" >
      firstname:
          <input type="text" formControlName="fname"><br>
      lastname:
          <input type="text" formControlName="lname">
          <button type="submit" (click)="Submit(testform)">Submit</button>
</form>

I hope anyone knows the issue. Thank you.

Share Improve this question edited Nov 19, 2019 at 5:41 Prashant Pimpale 10.7k10 gold badges50 silver badges87 bronze badges asked Jun 24, 2019 at 7:03 user11675217user11675217 6
  • Do you face any issue to do that? – Prashant Pimpale Commented Jun 24, 2019 at 7:11
  • i dont know how to do that – user11675217 Commented Jun 24, 2019 at 7:13
  • Check the posted answer – Prashant Pimpale Commented Jun 24, 2019 at 7:15
  • You should store the data in a service instead of local storage – Augustin R Commented Jun 24, 2019 at 7:17
  • will u explain how to do that? – user11675217 Commented Jun 24, 2019 at 7:54
 |  Show 1 more ment

1 Answer 1

Reset to default 7

To set the value:

JSON.stringify() is needed if you are storing an object.

localStorage.setItem('form-data', JSON.stringify(this.testform.value));

and to get the stored object from localStorage:

let formValue = JSON.parse(localStorage.getItem('form-data'))
发布评论

评论列表(0)

  1. 暂无评论