Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

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

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 my page or moved to another page the dropdownlist will clear automatically.

I want to retain the dropdownlist value after refresh the page or moved to another page.

I tried like this but this is not help ful.

HTML

<select id="Facility_ID" required typeof="text" name="Facility Name" ng-options="Role.FacilityName for Role in Roles"
                    form="DistanceMatrixFormId" class="form-control" ng-model="Role._id" ng-selected="getFacilityID(Role._id.FacilityName)">
            </select>

Controller.js

 $scope.getFacilityID = function (data) {
     localStorage.setItem("FacilityName", data);
     var getfacility = localStorage.getItem("FacilityName");
 }

i refered this link but it is not help ful

I don't know how to retain the value. can any one fixed me.

8 months ago · Santiago Trujillo
2 answers
Answer question

0

You cannot put an object into the local storage, you must store strings inside local storage.

If you want, you can have an implementation I did here : How to add local storage in angular?

For your current code, you don't need to use ng-selected. ng-model is enough.

<select id="Facility_ID" required typeof="text" 
        name="Facility Name" 
        ng-options="Role.FacilityName for Role in Roles"
        form="DistanceMatrixFormId" 
        class="form-control" 
        ng-model="Role._id"
        ng-change="updateLocalStorage()">
</select>

And inside your angular controller, what you can do is the following :

myApp.controller('controllerName', ['$scope', 'LocalStorageService',
     function($scope, LocalStorageService){

     // After initialization of your "Role" object

     $scope.Role._id = LocalStorageService.retrieve('mySelectValue');

     $scope.updateLocalStorage = function(){
         LocalStorageService.store('mySelectValue', $scope.Role._id);
     }

}])
8 months ago · Santiago Trujillo Report

0

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);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<div ng-app="App" ng-controller="MyCtrl">
  <select ng-model="selected" ng-options="x for x in options">
  </select>
</div>

8 months ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.