如何使用angularjs在本地存储中保存json响应? [英] How to save json response in local storage using angularjs?
本文介绍了如何使用angularjs在本地存储中保存json响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个返回json响应的api,我想将该json响应存储在localstorage中,以便在我的另一个使用angularjs的html页面中使用该响应.
I have api which return json response and I want to store that json response in localstorage to use that response in my another html page using angularjs.
这是我的返回json响应的代码....
Here is my code which return json response....
QAApp.controller('SearchCtrl', function ($scope, $http, $location) {
$scope.search = function (searchtag) {
var request = $http({
method: 'GET',
url: server + 'api/question/tagged/' + searchtag,
});
request.success(function(data, status, headers, config) {
console.log(data);
$scope.qa = data;
});
}
});
请告诉我如何存储...
Please tell me how can I store it...
推荐答案
在您的request.success()上使用
On your request.success(),use
window.localStorage['storageName'] = angular.toJson(data);
然后您可以通过以下方式访问本地存储中的数据
Then you can access the data in localstorage by
var accessData = window.localStorage['storageName'];
这篇关于如何使用angularjs在本地存储中保存json响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文