使用 ngStorage 我可以使用 var 作为存储 name/key 吗?

Using ngStorage can I use a var as the stored name/key?

我刚刚接触 ngStorge,所以这可能很简单。

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

这是他们的 read/write 演示:

<!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

我想为存储的 name/key 使用 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>

提前致谢。

如果我没理解错的话,你可以在下面的代码中使用方括号表示法:

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