【问题标题】:Using ngStorage can I use a var as the stored name/key?使用 ngStorage 我可以使用 var 作为存储的名称/密钥吗?
【发布时间】:2015-07-04 06:16:08
【问题描述】:

我刚刚开始使用 ngStorge,所以这可能很简单。

我正在使用https://github.com/gsklee/ngStorage

这是他们的读/写演示:

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <script data-require="angular.js@1.1.5" data-semver="1.1.5" src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
    <script src="https://rawgithub.com/gsklee/ngStorage/master/ngStorage.js"></script>

    <script>
      angular.module('app', [
        'ngStorage'
      ]).

      controller('Ctrl', function(
        $scope,
        $localStorage
      ){
        $scope.$storage = $localStorage.$default({
          x: 42
        });
      });
    </script>
  </head>

  <body ng-controller="Ctrl">
    <button ng-click="$storage.x = $storage.x + 1">{{$storage.x}}</button> + <button ng-click="$storage.y = $storage.y + 1">{{$storage.y}}</button> = {{$storage.x + $storage.y}}
  </body>

</html>

http://plnkr.co/edit/3vfRkvG7R9DgQxtWbGHz?p=preview

我想为存储的名称/密钥使用 var。

所以而不是:

<button ng-click="$storage.x = $storage.x + 1">{{$storage.x}}</button>

我想使用:

var testx = 'x';

并使用 testx insted of .x

<button ng-click="$storage.x = $storage.x + 1">{{$storage.x}}</button>

提前致谢。

【问题讨论】:

  • 如果将变量设置为键值呢?或者这不是你想要的?

标签: angularjs local-storage ng-storage


【解决方案1】:

如果我理解正确,您可以使用如下代码中的括号表示法:

<button ng-click="$storage[ testx ] = $storage[ testx ] + 1">{{$storage[ testx ]}}</button>

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2017-02-27
    • 2022-12-18
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多