gpt4 book ai didi

angularjs - 在 Angular 中使用资源将数据存储在 json 文件中

转载 作者:行者123 更新时间:2023-12-04 04:44:13 25 4
gpt4 key购买 nike

伙计们,我的网站上有一个表单,我想将其数据存储在 json 文件中。

这是表单的代码:

 <form>
<input ng-model="obj.firstname">
<input ng-model="obj.lastname">
<button ng-click="storedata()">Click here to store data</button>
</form>

我的 Angular 代码如下:
var myApp = angular.module('app', ['ui.bootstrap.dialog','ngResource']);

myApp.controller('TestCtrl', function($scope,$dialog,TestResource) {
$scope.obj = {};
$scope.obj.firstname = "Mahatma";
$scope.obj.lastname = "Gandhi";

$scope.storedata = function() {
console.log("Storing Data now");
TestResource.save($scope.obj);
console.log("Data should have been stored");
}
});


myApp.factory('TestResource', ['$resource', function($resource) {
return $resource('test.json', {}, {} );
}]);

问题是数据没有被存储。我在这里错过了什么吗?
这是一个 plunkr: http://plnkr.co/edit/gist:3662702

最佳答案

ngResource 是一个 Angular 服务,旨在与 RESTful 服务器端数据源交互(参见 ngResource docs)。 angular js 教程倾向于引用本地 JSON 文件,因为它们是独立的示例,任何人都可以在不需要后端服务器的情况下在本地下载和运行。但是您会注意到这些教程仅从 JSON 文件中读取数据,而无法更新它们。

如果您想在客户端保存数据,请查看 LocalStorage ( http://diveintohtml5.info/storage.html )。

如果你想保存数据服务器端,你需要设置一些后端服务(通过 NodeJS、PHP、.NET、Ruby、Python 和许多其他框架......)

关于angularjs - 在 Angular 中使用资源将数据存储在 json 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18445423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com