Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  163] [ 3]  / answers: 1 / hits: 19263  / 10 Years ago, wed, july 23, 2014, 12:00:00

I am using $resource.query() to fetch an JSON array from a JSON file. I can change the data with $save, but the JSON file is not actually updated. Can't find solutions after googling for the entire morning. Not sure if it is possible to update local json file with AngularJS. Any help would be appreciated.



Here is my code.



getData: function(datatype){
var url = data/ + datatype + .json;
return $resource(url, {}, {
query: {
method: GET,
isArray: true
}
});
},


Call the getData() function in another service function:



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
},
...
]

More From » json

 Answers
84

This Stackoverflow question and answer might be of some use to you: Edit a file using javascript



In short; Javascript typically can't write to a file unless you're making a post back to a server which can then do the update to your json file.



Hope this helps.


[#70066] Tuesday, July 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
havenbilliec

Total Points: 324
Total Questions: 106
Total Answers: 94

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;