如何更新/添加到使用angularJS一个JSON文件? [英] How do I update/add to a JSON file using angularJS?

查看:128
本文介绍了如何更新/添加到使用angularJS一个JSON文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个本地JSON文件,我能够得到使用从文件中的数据

I've created a local JSON file and am able to get data from the file using

app.controller('appCtrl', function($scope, $http){
  $http.get('employees.json').success(function(data){
  $scope.employees=angular.fromJson(data.employees);
  console.log($scope.employees);
});  //this is where the data from the json file is taken and stored into $scope.employees. works fine
}

我一直试图做一个 $ scope.employees.push 写一个相当于HTTP POST调用,但它只是(可以理解)更新的价值局部变量。

I've been trying to write an equivalent http POST call by doing a $scope.employees.push, but it is only (understandably) updating the value of the local variable.

如何更新或添加值,原来的 employees.json 文件?

How do I update or add values to the original employees.json file?

推荐答案

我不认为这是可能直接从角做到这一点。还有,你融入你的应用程序,然后你就可以有角利用一些其他来源。

I don't think it's possible to do that directly from Angular. There are some other sources that you integrate into your app, which you can then have Angular leverage.

  • Exploring the FileSystem API (HTML5) - verify which browsers support this.
  • Another options is jQuery.twFile.

您也可以只将其存储为 的localStorage 和访问它,你将几乎任何其他对象。 (取决于尺寸),这将是我会朝着更瘦的方法。否则,我一直有服务器端做到这一点。

You could also just store it as localstorage and access it as you would just about any other object. This would be the method that I'd lean towards more (depending on the size). Otherwise I've always had to have the server-side do it.

这篇关于如何更新/添加到使用angularJS一个JSON文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆