Skip to content
Advertisement

loading different values into select angular

Hi I have a problem with loading data from localStorage into a select component. I would like each select after selecting different options to be saved in localStorage and after refreshing the page, the value from the select will be remembered, currently the last option is remembered and loaded to all

app.component.html

JavaScript

select.component.html

JavaScript

select.component.ts

JavaScript

Advertisement

Answer

It will be much easier if you add an input parameter.

My solution:

  1. Add @Input() side: 'left' | 'right' | 'center'; at your custom select component.

  2. Change the saveData function as:

    saveData(e) {

    JavaScript
  3. Change app.component.html as:

JavaScript

Edit:

  1. Now, to set stored data from localStorage, add these lines at select component’s ngOnInit hook:

JavaScript

Note: I’ve added an extra Save button after each select for test purposes.

Result:

Desired result

I’ve demonstrated a broken(!) but working solution at Stackblitz.

Advertisement