Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在本地存储中创建javascript数组和变量-AngularJS_Javascript_Angularjs - Fatal编程技术网

在本地存储中创建javascript数组和变量-AngularJS

在本地存储中创建javascript数组和变量-AngularJS,javascript,angularjs,Javascript,Angularjs,谁能告诉我如何使用angularJS在localstorage中创建数组,如myarray=[],以及其他变量, 我创建了如下所示的变量 <!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.j

谁能告诉我如何使用angularJS在localstorage中创建数组,如
myarray=[]
,以及其他变量, 我创建了如下所示的变量

<!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  // variable
        });
      });
    </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>

角度模块('app'[
“NGS存储”
]).
控制器('Ctrl',函数(
$scope,
$localStorage
){
$scope.$storage=$localStorage.$default({
x:42//变量
});
});
{{$storage.x}}+{{$storage.y}={{{$storage.x+$storage.y}}

它支持json,您可以存储除json不支持的内容以外的任何内容:


角度模块('app'[
“NGS存储”
]).
控制器('Ctrl',函数(
$scope,
$localStorage
){
$scope.$storage=$localStorage.$default({
x:42,
数组:[]
});
});

阵列之间的克隆如何对于克隆,您可以使用angular.copy
<script>
      angular.module('app', [
        'ngStorage'
      ]).

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