我正在使用$ resource.query()从JSON文件中获取JSON数组。我可以使用$ save更改数据,但是JSON文件实际上并未更新。在整个上午进行谷歌搜索后找不到解决方案。不知道是否可以使用AngularJS更新本地json文件。任何帮助,将不胜感激。
这是我的代码。
getData: function(datatype){
var url = "data/" + datatype + ".json";
return $resource(url, {}, {
query: {
method: "GET",
isArray: true
}
});
},
在另一个服务函数中调用getData()函数:
var post = this.getData("jobs").query(function(){
angular.forEach(staffs, function(staff){
for(var i = 0; i < post.length; i++){
if(post[i].id == jobId){
alert(post[i].name); //Here it shows the original name "names and numbers"
post[i].name = "changed";
post[i].$save();
alert(post[i].name); //Here it shows the new name "changed"
//but the data json file is not changed
continue;
}
}
});
});
jobs.json:
[
{
"id": 554114,
"name": "names and numbers",
"facility_id": 0
},
...
]
这个Stackoverflow问题和答案可能对您有用:使用javascript编辑文件
简而言之; 除非您将帖子发回到服务器上,然后服务器可以对json文件进行更新,否则Javascript通常无法写入文件。
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句