【问题标题】:Keep the state of checkbox angularjs保持复选框angularjs的状态
【发布时间】:2014-06-14 06:01:58
【问题描述】:

我有这个多重复选框。

<div ng-repeat="setting in settings">
    <input type="checkbox" name="setting.name" ng-model="setting.value">{{setting.name}}

在控制器中

$scope.settings = [{
           name: 'OrderNr',
           value: ''
       }, {
           name: 'CustomerNr',
           value: ''
       }, {
           name: 'CatalogName',
           value: ''
       }, {
           name: 'OrderDate',
           value: ''
       }, {
           name: 'OrderState',
           value: ''
       }];

当我点击保存按钮时,我调用此函数将数据存储在本地存储中

$scope.saveSetting = function() {
    var json = angular.toJson($scope.settings);
    localStorageService.add('settings',json);

};

当我重新加载页面时如何保留我的设置?

【问题讨论】:

    标签: angularjs checkbox local-storage


    【解决方案1】:

    您只需要在控制器加载时设置 $scope.settings。

    var myCtrl = function($scope) {
        if(localStorage.settings) {
            $scope.settings = angular.fromJson(localStorage.settings);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我会使用 ngStorage 指令。我在我的应用程序中使用它。您只需使用 ngStorage 将设置设置为您的存储,然后在控制器加载时通过 ngStorage 设置设置的初始值。

      https://github.com/gsklee/ngStorage

      【讨论】:

        【解决方案3】:

        如何使用 $cookies。

        看看angularjs docs

        基本上,您需要做的就是将服务作为依赖项注入到控制器中,并将其用作对象。

        在您的控制器中:

        $scope.saveSetting = function() {
            $cookies.settings = $scope.settings;
        };
        

        并使用它们:

        $scope.loadSetting = function() {
             $scope.settings = $cookies.settings;
        };
        

        如果它适合你,请告诉我。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-08-25
          • 2016-06-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多