从$ localstorage中检索变量

时间:2016-04-29 16:47:27

标签: javascript jquery angularjs local-storage bootstrap-table

我正在使用bootstrap table和ngStorage。我尝试做的是将搜索保存到本地存储,并在刷新表后将其恢复。 这是我的简单代码:

JS

 //Assigning $localstorage to $session.storage
        $scope.storage = $localStorage;
        $scope.saveSearch = function () {
            $scope.child = prompt('Set your filter.');

            $('#table').on('search.bs.table', function (e, text){
                $scope.storage = text
            });

        };
        $scope.loadSearch = function() {

            $scope.data = $scope.storage;

        };

HTML

<div id="toolbar">
<div style="display: -webkit-box">
<button id="new" class="btn btn-default">New</button>
<button id="edit" class="btn btn-default">Edit</button>
<button id="remove" class="btn btn-default">Delete</button>
<button class="btn btn-default " ng-click="saveSearch()">Save Filter</button>
<button class="btn btn-default " ng-click = "loadSearch()">Load Filter</button>
<button id="button" class="btn-primary btn">Load Table</button>
<pre>{{data}}</pre></div>
</div>
table id="table"
      class="table-striped"
                        data-toggle="table"
                        data-toolbar="#toolbar"
                        data-search="true"
                        data-show-columns="true"
                        data-show-export="true"
                        data-filter-control="true"
                        data-events="operateEvents"
                        data-formatter="operateFormatter"
                        data-response-handler="responseHandler"
                        >
                        </table>

通过这段代码,当我点击按钮加载过滤器时,我变回了变量。您可以在“加载表”按钮的右侧看到它。

enter image description here

但是当我试图将此变量推送到搜索时。

  $scope.loadSearch = function() {
            $('#table').on('search.bs.table', function (e, text){
                text.push($scope.storage);
                console.log($scope.storage)
            });
            $scope.data = $scope.storage;
        };

我的功能根本不起作用。如果有人能解释我的错误,我感激不尽?

My plunker, where you can see the problem

With this plunker,你可以将localstorage中的值放到搜索bs表中,但它不会触发。

2 个答案:

答案 0 :(得分:1)

$scope.storage = $locastorage将整个$localstorage对象分配给变量。我认为这不是你想做的事。

阅读特定变量$localstorage

$scope.myvar = $localstorage.localvar

此外text.push($scope.storage);未向$scope.storage分配任何内容。 myarray.push(myvar)myvar添加到myarray

将某些内容放入$scope.storage

  • $scope.storage = text
  • $scope.storage.mykey = text

答案 1 :(得分:1)

将文本保存到本地存储空间

$scope.storage.text = text;

而不是检索文本

$scope.data = $scope.storage.text