localStorage data disappears after the page is refreshed

Jun 30 2020 4:53 PM
I am using a ControlValueAccessor to change my div tag to a form element and I am using localStorage to set and get my data. I am having the problem that my product_name item isn't being saved and after I refresh the page, the product_name disappears. How can I change my code so that the data persists after a page refresh?
In the HTML template:
  1. <div class="product_name" name="p_name"></div>  
  2. <div name="product_name" (click)="add('Bike')" style="cursor:pointer;">Bike</div>  
In the controller:
  1. add(product_name){  
  2. localStorage.setItem("product_name", product_name);  
  3. (document.querySelector('.product_name') as HTMLElement).innerText = localStorage.getItem("product_name");  
  4. }  

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (2)