简体   繁体   English

使用AngularJS保存修改过的json文件

[英]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. 您可以使用多种选项。

  1. HTML5 localStorage API. HTML5 localStorage API。 That would probably be the easiest. 这可能是最简单的。 It is key-value based, with string keys and string values. 它是基于键值的,具有字符串键和字符串值。 It is also extremely portable. 它也非常便携。 Depending on how simple the app is, this may be the best. 根据应用程序的简单程度,这可能是最好的。 You set items either as a property of window.localStorage or using getKey / setKey . 您可以将项目设置为window.localStorage的属性或使用getKey / setKey There is a ton of documentation and tutorials online about this. 网上有大量关于此的文档和教程。
    • Caveat: On Windows Phone 7, you cannot use dot notation. 警告:在Windows Phone 7上,您不能使用点表示法。 You must use the latter method. 您必须使用后一种方法。
  2. Use the Cordova File API. 使用Cordova File API。 It is less consistent across operating systems than localStorage , but it is still fairly dependable. 它在操作系统上的性能不如localStorage ,但它仍然相当可靠。 Here's a link to more information. 这是一个更多信息的链接
    • Caveats: Beyond cross-operating system incompatibilities, you may also have to install it as a plugin. 注意事项:除了跨操作系统不兼容之外,您可能还必须将其安装为插件。 Here's the command: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM