Retain the dropdown value after the page is refresh or move to another page in angularjs

后端 未结 2 492
礼貌的吻别
礼貌的吻别 2021-01-23 08:46

let me explain my scenario. I am having the dropdownlist in my master page. if changed dropdownlist the data are changed depend upon the dropdownlist value.
If i refreshed m

2条回答
  •  感动是毒
    2021-01-23 09:33

    Here is an example that uses a service to store the selected value. Unfortunately the embedded demo does not work because of the sandboxing, but works when served as an application.

    angular.module(
      "App", []
    ).factory("MyStorage", function() {
      const key = "selected";
      return {
        getValue: function() {
          const stored = localStorage.getItem(key);
          return stored ? JSON.parse(stored) : null;
        },
        setValue: function(value) {
          localStorage.setItem(key, JSON.stringify(value));
        }
      };
    }).controller("MyCtrl", function($scope, MyStorage) {
      $scope.options = ["A", "B", "C"];
      $scope.selected = MyStorage.getValue();
      $scope.$watch("selected", newValue => {
        MyStorage.setValue(newValue);
      });
    });
    
    

提交回复
热议问题