javascript 如何在 angularjs 中创建和更新 JSON 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17036137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:51:34  来源:igfitidea点击:

How to create and update JSON file in angularjs?

javascriptjsonangularjs

提问by Mayur

I just want to create a simple app which creates, updates, and deletes items in JSON file using AngularJS. I have looked online, only found examples which takes JavaScript objects to edit, or delete JSON type data. But I want to use JSON as backend in AngularJS.

我只想创建一个简单的应用程序,它使用 AngularJS 创建、更新和删除 JSON 文件中的项目。我在网上查看过,只找到了使用 JavaScript 对象来编辑或删除 JSON 类型数据的示例。但我想在 AngularJS 中使用 JSON 作为后端。

Is there any examples out which can post data to JSON file?

是否有任何示例可以将数据发布到 JSON 文件?

Thanks in advance!

提前致谢!

回答by holographic-principle

Try something like:

尝试类似:

angular.module('myApp').factory('MyJsonService', function() {
  return $resource('/:myFile.json', {myFile: '@file'});
});

Then in your controller:

然后在您的控制器中:

$scope.myJsonContents = MyJsonService.get({file: 'myFilename'}); // keep in mind that this is async though