[英]Save modified json file with AngularJS
I'm creating a phonegap app with AngularJS that uses a .json file to store an array of entries. 我正在使用AngularJS创建一个phonegap应用程序,它使用.json文件来存储一系列条目。 My aim is to allow the user to mark some of them as favorites, and then use that data somewhere else (another page with the favorites only, for example).
我的目标是允许用户将其中一些标记为收藏夹,然后在其他地方使用该数据(例如,仅包含收藏夹的另一页面)。 First time doing a web development and I have to say I'm pretty new to web technologies such as AngularJS, JSON, PhoneGap, etc. (and I'm using them all), so maybe this question is a nonsense, but here I go anyway.
第一次进行Web开发,我不得不说我对AngularJS,JSON,PhoneGap等Web技术都很陌生(我正在使用它们),所以也许这个问题是废话,但在这里我无论如何。
I've tried to save that data in an IndexedDB, in a WebSQL DB, in WebStorage.... but those approaches seem to be complex combined with PhoneGap, so I'm trying now to modify a field in the .json file and save the modified file. 我试图将这些数据保存在WebS数据库中的IndexedDB,WebStorage中....但这些方法似乎与PhoneGap结合起来很复杂,所以我现在正在尝试修改.json文件中的字段,保存修改后的文件。
I know JS can't write files, but I was trying to send the file by POST instead to fill that gap. 我知道JS无法写文件,但我试图通过POST发送文件来填补这个空白。 Here's the code of the function I'm using:
这是我正在使用的函数的代码:
$scope.save = function() {
$http({
method: 'POST',
url: 'data.json', //this is the json file with all the information I use
data: $scope.json_data //this contains the modified data
}).success(function(response) {
addLog("Success message.");
}).error(function(response){
addLog("Error message.");
});
}
I don't want to write a lot of code to handle a request server side (I don't even know if PhoneGap will handle that...), since I honestly don't even know where to start to do that. 我不想写很多代码来处理请求服务器端(我甚至不知道PhoneGap是否会处理它......),因为我老实说甚至不知道从哪里开始这样做。 I don't know if a transformRequest or another simple thing can allow me to save the json file... Any insights on what to do?
我不知道transformRequest或其他简单的东西是否可以让我保存json文件...有什么见解? Should I save the modified .json file with some magic code, or maybe I'm looking at this problem in the wrong way and should go down a different path?
我应该用一些魔法代码保存修改后的.json文件,或者我是否以错误的方式看待这个问题并且应该走另一条道路?
Thanks! 谢谢!
You have several options you could use. 您可以使用多种选项。
localStorage
API. localStorage
API。 That would probably be the easiest. window.localStorage
or using getKey
/ setKey
. window.localStorage
的属性或使用getKey
/ setKey
。 There is a ton of documentation and tutorials online about this. localStorage
, but it is still fairly dependable. localStorage
,但它仍然相当可靠。 Here's a link to more information. cordova plugin add org.apache.cordova.file
. cordova plugin add org.apache.cordova.file
。 Here's some useful links: 这是一些有用的链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.