如何在角度js中存储和检索数据?

你能告诉我如何永久存储数据(例如本地存储)和检索数据。我想存储数据以便我再次获得。我正在进行客户端编程吗? 我谷歌它说它有persistance.js会这样做..我们可以使用吗?我试着做一个简单的例子来存储数据但不能那样。我想保存学生和class级名单。 这是我的傻瓜: http ://plnkr.co/edit/cLTIJfETGYENQyEdsu94?p =preview

// Code goes here var app = angular.module('appstart', ['ngRoute']); app.config(function($routeProvider) { // $locationProvider.html5Mode(true); $routeProvider .when('/home', { templateUrl: 'tem.html', controller: 'ctrl' }) .otherwise({ redirectTo: '/home' }); }); app.controller("ctrl", function($scope) { $scope.students = []; $scope.add = function() { $scope.students.push({ inputName : angular.copy($scope.inputName), inputclass : angular.copy($scope.inputclass) }); $scope.inputclass=''; $scope.inputName=''; } } ); 

谢谢

你可以在这里获取角度本地存储: https : //github.com/grevory/angular-local-storage

这样可以很容易地使用浏览器本地存储,我已经使用一个使用示例更新了您的plunker: http ://plnkr.co/edit/W06UnVysGx0FMVnITMZD

 app.controller("ctrl", ['$scope','localStorageService', function($scope, localStorageService) { $scope.students = []; $scope.add = function() { $scope.students.push({ inputName : angular.copy($scope.inputName), inputclass : angular.copy($scope.inputclass) }); $scope.inputclass=''; $scope.inputName=''; } $scope.loadFromStorage = function(){ $scope.students = localStorageService.get('studentsList'); if($scope.students == null) $scope.students = []; } $scope.saveToStorage = function(){ localStorageService.set('studentsList', $scope.students); } }]); 

请记住,这不会在网络上保留您的数据,如果您想这样做,您应该设置一个网络服务器,您可以从中发布和获取数据。 希望有所帮助。

通常你有很多选择来保持数据的持久性并存储它,在这里我知道:

 1 - localStorage or sessionStorage or IndexedDB 2 - cookie if your data is < 4kb (cookie max size) 3 - server side (mysql, redis, mongo and noSQL,json or other extension files etc) 4 - client side (json file or whatever file format you want/can develop) 

显然这些function中的一些并不总是被支持,请在这里查看您喜欢支持或不支持caniuse.com的function

只需选择适合您需求的那个,并创建一个dataFactory来存储,删除,检索数据:)

你需要一个数据库。 网络存储仅用于临时事物。

如果您有后端,则可以使用服务和$http来创建CRUD应用程序。

您使用Angular将数据发送到执行持久性的后端。