【问题标题】:How do I update/add to a JSON file using angularJS?如何使用 angularJS 更新/添加到 JSON 文件?
【发布时间】:2014-04-07 21:47:30
【问题描述】:

我创建了一个本地 JSON 文件,并且能够使用

从文件中获取数据
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 调用,但它只是(可以理解地)更新局部变量的值。

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

【问题讨论】:

  • 您不能从客户端执行此操作(它甚至超出了 Angular 的能力)。您必须在服务器端编写一个处理程序,然后将post 数据返回给它以将其存储在原始employees.json 文件中。
  • 是的,但是 JSON 文件存储在本地,在同一台计算机上。仍然需要处理程序吗?如果是这样,处理程序可以用 JS 编写还是使用 angular 编写?
  • 如果您更喜欢 javascript,可以使用 node.js。 Angular 是一个仅限客户端的框架,它不支持服务器端。(AFAIK)
  • 你有想过这个吗?

标签: javascript json angularjs


【解决方案1】:

我认为直接从 Angular 执行此操作是不可能的。您还可以将其他一些资源集成到您的应用中,然后您就可以利用 Angular。

您也可以将其存储为localstorage 并像访问任何其他对象一样访问它。这将是我更倾向于的方法(取决于大小)。否则我总是不得不让服务器端来做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2015-07-20
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多