我知道如何在 session /本地存储中存储数据..window.localstorage(key, value)
但是现在我使用的是 Angular,你能告诉我如何在 session 中以 Angular 存储数据吗或本地或表 SQLite 中。
我做了一个演示,其中我得到了 val 但没有商店。
http://plnkr.co/edit/7DaMFWLpFh5lWMHemuQ1?p=preview
// Code goes here
/**
* Created with JetBrains WebStorm.
* User: naveenkumar
* Date: 02/08/14
* Time: 6:29 AM
* To change this template use File | Settings | File Templates.
*/
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){
var information={};
$scope.login =function(){
var email=$('#inputEmail').val();
var password=$('#inputPassword').val();
alert(email +":"+password)
information.email= email;
information.password=password;
alert(JSON.stringify(information))
}
})
最佳答案
就 localStorage
而言,最佳实践是将与“外部世界”(数据库/模型)的交互放在 AngularJS Service 中。 .
This JSFiddle应该可以帮助你。我从 Plunkr 中删除了所有其他内容,以展示重要的内容。
我们使用服务,使代码模块化并且不属于任何一个 Controller 。除此之外,没什么特别的。本质上是一个全局类。
代码如下。我们以类似的方式声明服务。它成为一个单例,我们可以将其放入任何 Controller ,就像我对“ctrl” Controller 所做的那样。
app.service("LocalStorService", function(){
this.store = function(key, value){
window.localStorage.setItem(key, value);
};
this.retrieve = function(key) {
return window.localStorage.getItem(key);
}
});
然后在 Controller 中......
app.controller("ctrl", function ($scope, LocalStorService) { ...
如果我正确地将其理解为“代码组织”问题,这就是您需要做的全部事情。
关于javascript - 如何在 session 中或本地以 Angular 存储数据..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25091132/