在页面重新加载后检索所选的复选框值

时间:2016-07-04 15:00:54

标签: angularjs cookies checkbox

我正在尝试使用cookie来隐藏和显示表中的列,而不是将所选的复选框值存储在数据库中。我遇到的问题是,当用户再次启动模式时,我无法显示先前选择的复选框值。我正在尝试使用 ng-init 来取回值,并将复选框设置为 true (如果已选中)。

模态(尝试1)

<div class="modal-header">
    <h3 class="modal-title">Additional Collections Columns</h3>
</div>
<div class="modal-body">
    <h4>Select addional columns from the list below to display in 'Collections' list</h4><br/>

    <div class="row">
    <form name="collectionsColumnsForm">
        <div class="col-sm-6">
            <ul>
                ...
                <li>
                    <label class="control c control--checkbox">
                        <input type="checkbox" name="phone" ng-model="columns.coll_phone" value="1" ng-init="columns={coll_phone:'{{cookie_coll_phone}}'}">
                        Phone
                        <div class="control__indicator"></div>
                    </label>
                </li>
                ...
            </ul>
        </div>
        </form>
    </div>
    <br>
</div>

<div class="modal-footer">
    <button class="btn btn-danger" type="button" ng-click="cancel()">Cancel</button>
    <button class="btn btn-info" type="button" ng-click="save()">Save</button>
</div>

模态(尝试2)

<div class="modal-header">
    <h3 class="modal-title">Additional Collections Columns</h3>
</div>
<div class="modal-body">
    <h4>Select addional columns from the list below to display in 'Collections' list</h4><br/>

    <div class="row">
    <form name="collectionsColumnsForm">
        <div class="col-sm-6">
            <ul>
                ...
                <li>
                    <label class="control c control--checkbox">
                        <input type="checkbox" name="phone" ng-model="columns.coll_phone" value="1" ng-init="columns={columns.coll_phone:'{{cookie_coll_phone}}'}">
                        Phone
                        <div class="control__indicator"></div>
                    </label>
                </li>
                ...
            </ul>
        </div>
        </form>
    </div>
    <br>
</div>

<div class="modal-footer">
    <button class="btn btn-danger" type="button" ng-click="cancel()">Cancel</button>
    <button class="btn btn-info" type="button" ng-click="save()">Save</button>
</div>

模态(尝试3)

<div class="modal-header">
    <h3 class="modal-title">Additional Collections Columns</h3>
</div>
<div class="modal-body">
    <h4>Select addional columns from the list below to display in 'Collections' list</h4><br/>

    <div class="row">
    <form name="collectionsColumnsForm">
        <div class="col-sm-6">
            <ul>
                ...
                <li>
                    <label class="control c control--checkbox">
                        <input type="checkbox" name="phone" ng-model="columns.coll_phone" value="1" ng-init="columns=cookie_coll_phone">
                        Phone
                        <div class="control__indicator"></div>
                    </label>
                </li>
                ...
            </ul>
        </div>
        </form>
    </div>
    <br>
</div>

<div class="modal-footer">
    <button class="btn btn-danger" type="button" ng-click="cancel()">Cancel</button>
    <button class="btn btn-info" type="button" ng-click="save()">Save</button>
</div>

在上面的代码中,用户可以选择任何可用的选项。一旦用户选择了一个或多个复选框,那么该值就会被传递给angularjs控制器,然后在其中设置一个cookie。每当我检查浏览器工具或视图本身时,我从cookie中获得的值都是 true

Angularjs控制器

...

// stored cookie columns for collections table
...
$scope.cookie_coll_phone = $cookies.get('coll_phone');
...

// add more columns to collections list
$scope.collectionColumns = function() {

    var modalInstance = $uibModal.open({
       animation: true,
       templateUrl: '/js/modals/collection-columns.html',
        size: 'md',
       resolve:{
       },
      controller: function ($scope,$uibModalInstance) {

        ...
        $scope.cookie_coll_phone = $cookies.get('coll_phone');
        ...


        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };

        $scope.save = function() {

            ...
            $cookies.put('coll_phone', $scope.columns.coll_phone);
            ...

            toaster.pop('success', 'Success!', 'Additional column(s) have been added.');
            location.assign('/collections');
            $uibModalInstance.close();
        };

      }
   });

    modalInstance.result.then(function (data) {

    },function () {

    });
};
...

enter image description here

非常感谢任何有关此问题的帮助

1 个答案:

答案 0 :(得分:0)

我正在将复选框值保存到localStorage。 在控制器中只检查是否有我的localStorage属性。这是我的3个复选框的示例。

vm.checkedCheckBoxes = window.localStorage.checkboxes ? JSON.parse(window.localStorage.checkboxes) : [false, false, false];

之后,我只是使用&#39; ng-checked&#39;在html。