javascript - 记住模式中选中的复选框

标签 javascript html angularjs checkbox modal-dialog

这就是我需要的场景。当我打开模式框并选中一些复选框时,下次打开时我希望从第一次开始就选中复选框。下面是我的代码。

主控模态功能

  function openModal() {
            vm.modalInstance = $uibModal.open({
                animation: true,
                size: 'lg',
                templateUrl: "/template.html",
                controller: 'ModalController as vm',
                resolve: {
                    refFY: function () {
                        return vm.refFY;
                    }
                }
            });

            vm.modalInstance .result.then(function (selectedItem) {
                $log.info('Modal ok-ed ', selectedItem);
                vm.fySelections = selectedItem;
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }

模态 Controller

function ModalController($uibModalInstance, $log, refFY) {
        var fvm= this;
        fvm.refFY = refFY;
        fvm.fySelections = [];

        $log.info(fvm);

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

        function confirmFYSelectionModal() {
            $log.debug(fvm.fySelections);
            $uibModalInstance.close(fvm.fySelections);
        }

        function selectAll() {
            fvm.fySelections = angular.copy(fvm.refFY);
        }

        function resetAll() {
            fvm.fySelections = [];
        }

        fiscalyearvm.dismissFYSelectionModal = dismissFYSelectionModal;
        fiscalyearvm.confirmFYSelectionModal = confirmFYSelectionModal;
        fiscalyearvm.selectAll = selectAll;
        fiscalyearvm.resetAll = resetAll;
    }

** 模态视图主体**

    <div class="modal-body">
            <div class="panel">
                <div class="panel-body">
                    <div class="col-md-12" data-ng-repeat="fy in fvm.refFY" data-ng-show="fvm.refFY.length > 0">
                        <div class="ckbox ckbox-primary">
                            <label>
  <input type="checkbox" data-checklist-model="fvm.fySelections" data-checklist-value="fy">
                            {{fy.year}}
                        </label>
                    </div>
                </div>
            </div>
        </div>
</div>

最佳答案

我建议将复选框的状态存储在本地存储中。这个模块看起来非常可靠:https://github.com/grevory/angular-local-storage

您需要通过 setStorageType() 在该模块中配置 session 存储选项,因为 session 存储仅对 session 而言是持久的,而本地存储是持久的,直到您清除该 session 的浏览数据为止页。

或者,您可以直接访问 sessionStorage() API:https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

这有一个很好的方法,可以使用已添加到 Angular 范围中的模态来预填充输入:Angular js init ng-model from default values

不要使用文本值,而是使用 bool 值来设置 ng-checked 属性,这样您就可以开始工作了。

如果有问题请评论。

关于javascript - 记住模式中选中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36187198/

相关文章:

html - 使用 flexbox 为 li 添加边距

css - 在动态选项对象上删除 Angular 上的空白选项

javascript - 在 jQuery 中简洁地添加自定义事件触发器

php - 尝试创建图像表时出现 CSS 问题

html - 当其中有不同的列数时,如何制作 Bootstrap 3 样式表来填充其行?

javascript - AngularJS HTTP POST

javascript - 将数组显示为平均堆栈中的图像(节点+ Angular 等..)

javascript - 将 jQuery Datatable 导出到 Excel 时的表格单元格格式

javascript - 使用正则表达式在字符串模式出现前后插入文本

javascript - 如何在脚本运行时显示加载图像?