【发布时间】:2016-06-05 20:50:01
【问题描述】:
随时告诉我离开(我是 Angular 和 javascript 的新手),但我该如何开始将这个 angularJS 保存到 localStorage?通过在 add() 中创建一个保存函数可以相对轻松地完成一些事情吗?我已经看到 Angular 有自己的本地存储,但是没有它可以做到吗?笔:http://codepen.io/kiddigit/pen/LZVXMV
angular.module('app', []);
angular.module('app').controller("MainController", function(){
var vm = this;
vm.title = 'Movie Database';
vm.searchInput = '';
vm.shows = [
{
title: 'Game of Thrones',
actor: 'Dudes',
year: 2011,
favorite: true
},
{
title: 'Walking Dead',
actor: 'More dudes',
year: 2010,
favorite: false
},
{
title: 'The Goonies',
actor: 'The Coreys',
year: 2002,
favorite: true
},
{
title: 'Firefly',
actor: 'Dunno',
year: 2002,
favorite: false
},
{
title: 'Bullit',
actor: 'McQueen',
year: 2013,
favorite: true
},
];
vm.orders = [
{
id: 1,
title: 'Year Ascending',
key: 'year',
reverse: false
},
{
id: 2,
title: 'Year Descending',
key: 'year',
reverse: true
},
{
id: 3,
title: 'Title Ascending',
key: 'title',
reverse: false
},
{
id: 4,
title: 'Title Descending',
key: 'title',
reverse: true
}
];
vm.order = vm.orders[0];
vm.new = {};
vm.addShow = function() {
vm.shows.push(vm.new);
vm.new = {};
};
});
【问题讨论】:
-
您的问题是什么?您想在 LocalStorage 中保存什么(“this AngularJS”没有意义。AngularJS 是一个框架。您可以在 LocalStorage 中存储的是字符串。对象可以使用 JSON 轻松转换为字符串)。不,Angular 没有“自己的 LocalStorage”。
-
我猜这句话有误会。 Angular 有一个
localStorage的包装模块。但正如@JB Nizet 所说,它没有自己的localStorage。 -
感谢您说清楚。我是一个 obv 还是有点迷茫。