0

I have an angular application with a page with a form inside. In that page there is also a link to an external website. In the external website the user does something and then is directed back to my page.

I would like the data the user initially adds to my page's form to be saved so that when the user comes back from the external website, he will find the initial page with all the information he inserted.

The only solution that comes to mind is cookies/localStorage. So for example I would have to save all the data in the localStorage, go to the external website, come back to my page, and load the data from the localStorage.

Is that the only way or is there a better solution?

Thank you!

  • 2
    There are other ways, but they're complicated. Saving and retrieving from Local Storage is super simple. That's not a bad thing. If you find that it works for what you're doing here, go for it. – CertainPerformance Feb 20 '22 at 19:54
  • LocalStorage is ideally suited to this. I would iterate through the entered data, construct an object from the key/values extracted, JSON.stringify the object and set it to localStorage with a unique key. I'd struggle to find a simpler way as you'll have to convert the object to a string to store it as a file by any other method anyway. – Dave Pritlove Feb 25 '22 at 18:14

1 Answers1

0

Retrieving Data from route

You could set your desired data in the route and retrieve it in your Angular application. For Example, you want to check whether or not your user has checked something once they have been redirected to the external website; all you need to do is set a query param and then check it once the user comes back to your original web application.

e.g.:

yourwebsite.com/home?hasSelected=true

And in your component, you can access it like below:

constructor(private readonly activatedRoute: ActivatedRoute) { }

ngOnInit() {
    this.param1 = this.activatedRoute.snapshot.paramMap.get('param1');
}

You can learn more about getting data from query params Here.

Hossein Mousavi
  • 3,118
  • 3
  • 16
  • 35