1

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-

  1. (most importantly) test.json never changes when I update(), maybe because
  2. 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.
  3. 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.

Community
  • 1
  • 1
BeenGaming
  • 11
  • 3
  • To actually do something with the data serverside you need to have some serverside code that stores it somewhere, like in a database. – Jan Jul 06 '15 at 22:53
  • 1
    I think you answered your own question. You can't just write to a local file. You need server side code to do that. It is pretty simple though. You might want to look into json-server (https://github.com/typicode/json-server) – o4ohel Jul 06 '15 at 22:56
  • i think @o4ohel comment would _be_ the accepted answer. – netalex Nov 16 '18 at 15:27

0 Answers0