将数组保存在 ng-storage AngularJS

Save array in ng-storage AngularJS

如何在angular中保存和显示数组?

示例:

$scope.save = function () {
    var myOby= [
    { name: 'A', address: '1' },
    { name: 'B', address: '2' },
    { name: 'C', address: '3' },
    { name: 'D', address: '4' }
    ];
    //save
}
$scope.load = function() {
    //show
}

有什么解决办法吗?

localStorage 支持数组和任何其他有效 JSON(对象、字符串、数字),但不支持示例函数。

Here is a plunkr demonstrating this

angular代码:

var app = angular.module('myApp', ['ngStorage']);

app.controller('myCtrl', function($scope, $localStorage) {
  $scope.text = "Hello!";
  $scope.save = function () {
    var myOby= [
    { name: 'A', address: '1' },
    { name: 'B', address: '2' },
    { name: 'C', address: '3' },
    { name: 'D', address: '4' }
    ];
    //save
    $localStorage.myKey =  myOby;
}
$scope.load = function() {
    //show
    $scope.restored_data = $localStorage.myKey;
}
});

正如上面评论中提到的,此信息在文档中很容易获得:Documentation