【问题标题】:Save array in ng-storage AngularJS在 ng-storage AngularJS 中保存数组
【发布时间】:2016-06-08 14:38:20
【问题描述】:

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

示例:

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

有什么办法吗?

【问题讨论】:

  • 您是否尝试过阅读相关文档? ngStoragelocalStorage。它有据可查和解释。你不明白什么?你有什么问题?
  • localStorage 支持数组,只要它们是有效的 JSON(即它们只包含对象、字符串和数字 - 不包含函数)。 plnkr.co/edit/2uj19YbRreiu9Y0N9jOL?p=preview
  • 在问这样的基本问题之前,您应该阅读文档。如果它更具体,那么在你的问题中更具体。
  • @cm92 Tnx men....把答案放在下面,我会标记你的例子...

标签: angularjs local-storage ng-storage


【解决方案1】:

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;
}
});

如上述 cmets 所述,此信息可在文档中轻松获得:Documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多