【问题标题】:Retain the dropdown value after the page is refresh or move to another page in angularjs页面刷新后保留下拉值或在angularjs中移动到另一个页面
【发布时间】:2017-09-23 16:16:00
【问题描述】:

让我解释一下我的情况。我的母版页中有下拉列表。如果更改了下拉列表,则数据的更改取决于下拉列表的值。
如果我刷新我的页面或移动到另一个页面,下拉列表将自动清除。

我想在刷新页面或移动到另一个页面后保留下拉列表的值。

我试过这样,但这无济于事。

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

我不知道如何保留价值。谁能帮我修一下。

【问题讨论】:

    标签: angularjs node.js mean-stack


    【解决方案1】:

    这是一个使用服务来存储所选值的示例。不幸的是,由于沙盒,嵌入式演示无法运行,但在作为应用程序使用时可以运行。

    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>

    【讨论】:

      【解决方案2】:

      您不能将对象放入本地存储中,您必须将字符串存储在本地存储中。

      如果你愿意,你可以有一个我在这里做的实现:How to add local storage in angular?

      对于您当前的代码,您不需要使用 ng-selected。 ng-model 就够了。

      <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>
      

      在您的角度控制器中,您可以执行以下操作:

      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);
           }
      
      }])
      

      【讨论】:

      • 嗨 Deblaton Jean-philippe:我收到了这个错误 [$injector:unpr] errors.angularjs.org/1.3.12/$injector/…
      • 我的项目中是否需要为localstorageservice添加任何js
      • @Vinoth 然后JSON.stringify他们
      • @Vinoth 在我的回答中,我为您提供了一个指向我 2 年前写的 LocalStorageService 的链接。我在这里的回答说明了如何在您的情况下使用它。所以是的,您需要像我在链接的答案中所做的那样创建一个服务
      • @DeblatonJean-Philippe:没有在我的 UI 中打开任何东西。我的 controller.js 页面出现错误。 _id 未定义。
      猜你喜欢
      • 2013-07-19
      • 2018-10-31
      • 2015-06-21
      • 2019-10-24
      • 2011-08-23
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多