Я просто мочу ногами ngStorge, так что это, наверное, просто.
Я использую https://github.com/gsklee/ngStorage.
Вот их демо-версия для чтения/записи:
<!DOCTYPE html>
<html ng-app="app">
<head>
<script data-require="[email protected]" 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 inst. Of.x
<button ng-click="$storage.x = $storage.x + 1">{{$storage.x}}</button>
Заранее спасибо.
Если я правильно понял, вы можете использовать обозначение скобок, как в приведенном ниже коде:
<button ng-click="$storage[ testx ] = $storage[ testx ] + 1">{{$storage[ testx ]}}</button>