将数组保存在ng-storage AngularJS中

时间:2016-06-08 14:38:20

标签: angularjs local-storage ng-storage

如何以角度保存和显示数组?

示例:

$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
}

任何解决方案?

1 个答案:

答案 0 :(得分:0)

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

Here is a plunkr demonstrating this

角度代码:

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