简体   繁体   中英

How to store reactive form data to localstorage?

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.

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'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM