sha*_*osh 2 javascript json angularjs
我正在使用$ resource.query()从JSON文件中获取JSON数组.我可以使用$ save更改数据,但JSON文件实际上并未更新.谷歌搜索整个上午后无法找到解决方案.不确定是否可以使用AngularJS更新本地json文件.任何帮助,将不胜感激.
这是我的代码.
getData: function(datatype){
var url = "data/" + datatype + ".json";
return $resource(url, {}, {
query: {
method: "GET",
isArray: true
}
});
},
Run Code Online (Sandbox Code Playgroud)
在另一个服务函数中调用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;
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
jobs.json:
[
{
"id": 554114,
"name": "names and numbers",
"facility_id": 0
},
...
]
Run Code Online (Sandbox Code Playgroud)
克里斯的上述答案似乎并不重要.最初的问题是关于使用angular的$ resource保存.不是javascript是否可以写入文件.
$ resource已经定义了"Post".因此,正确调用的$ resource.save()将使回发调用服务器.在服务器上,您仍然需要编写代码来保留发布的数据.但底线是$ resource允许Post.
shaosh似乎与$ resource.save存在问题
| 归档时间: |
|
| 查看次数: |
18876 次 |
| 最近记录: |