【问题标题】:Retrieve variable from $localstorage从 $localstorage 中检索变量
【发布时间】:2016-08-24 22:15:26
【问题描述】:

我正在使用 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>

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

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

  $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表中搜索,但是没有触发。

【问题讨论】:

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


    【解决方案1】:

    将文本保存到本地存储

    $scope.storage.text = text;
    

    然后比检索文本

    $scope.data = $scope.storage.text
    

    【讨论】:

    • 文本正在检索,问题是我无法推回该文本进行搜索
    • 你试过 text.push($scope.storage.text);
    • 如果我使用 $scope.storage.text 它给我的文本是未定义的
    • 您是否将文本保存到 storage.text 中?你必须保存文本才能加载它
    • 你的搜索代码是什么意思?它是 bs 表的一部分,通过这个 text 在这个函数 $('#table').on('search.bs.table', function (e, text){ }); 中,你可以找到搜索值
    【解决方案2】:

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

    读取$localstorage的特定变量做

    $scope.myvar = $localstorage.localvar
    

    此外,text.push($scope.storage); 不会将任何内容分配给 $scope.storagemyarray.push(myvar)myvar 添加到myarray

    把东西放进$scope.storage

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

    【讨论】:

    • 返回:TypeError: $scope.storage.push is not a function
    • 似乎是对的。我做了一些阅读: $scope.storage = $localstorage 将整个本地存储推送到这个变量中。请尝试$scope.storage = text
    猜你喜欢
    • 2019-08-20
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多