For now, all I'm trying to do is read in a simple JSON file, allow the user to edit the contents, and then save it back to the same file. I've read several answers like this, but I'm not dealing with form data or trying to cut out jquery and my understanding (probably the issue) is that it shouldn't be that complicated.
Working off of this answer, here's what I've got:
<div ng-controller="myCtrl as items">
<table>
<tr ng-repeat="item in items.testData">
<td><strong>Name:</strong></td>
<td><input ng-model="item"></td>
</tr>
</table>
<button ng-click="items.update()">Submit</button>
<p>{{items.msg}}</p>
</div>
In the app.js:
(function() {
var app = angular.module("myApp", []);
app.controller("myCtrl", ["$http", function($http) {
var list = this;
this.testData = [];
$http.get("test.json").success(function(data) {
list.testData = data);
});
this.msg = "";
this.update = function() {
$http.post("test.json", list.testData);
this.msg = "saved:" + JSON.stringify(list.testData);
};
}]);
})();
And finally the JSON, just so you see what I see:
[
"first item",
"second item",
"3rd item",
"And fourth"
]
I'm only working locally right now, so there shouldn't be any issue with my server failing to understand the output (as discussed here). I'm just going JSON to JSON.
Now, my observations-
- (most importantly) test.json never changes when I update(), maybe because
- testData doesn't seem to actually change, according to the output in msg. Is something wrong with my ng-model? It works in the above example.
- There's no error for trying to post to test.json, but if I instead set the destination as test-new.json, I get Access to restricted URI denied. Can I not actually write to a local file at all?
Hopefully, I'm right that this deserved its own question; nothing else I looked at seemed to answer just the basic problems I'm having. Any help greatly appreciated.