0

I've just started out with Angular, and I did the codeschool tutorial. I took it a step further and wanted to push form data to a json file.

I'm getting the correct response where the information is posted, but the file is not being updated. I've tried this locally and on a development server.

This is my directive code:

//---------------------------------------//
// Lets user add a review to site
app.directive("productAddReview", function($http) {
  return {
    restrict: 'E',
    templateUrl: 'directives/product-add-review.html',
    controller: function($http) {
        // gets a blank review
        this.review = {};

        // sets today's date as a variable
        this.review.date = Date.now();

        // adds a review
        this.addReview = function(product) {
            product.reviews.push(this.review);      // adds review to dom
            $http.post('../data/products.json', this.review) //updates json file
            this.review = {};                       // resets review upon completion
        };          
    },
    controllerAs: 'reviewCtrl',
 };
});

Example json response upon review submit:

author: "[email protected]"

body: "this is a test"

date: 1443540061192

stars:  5

Source: {"date":1443540061192,"stars":5,"body":"this is a test","author":"[email protected]"}
1
  • 2
    your server has to update the file. but if its just a file on disk, then nothing will happen. nothing to do with angular per se. Commented Sep 29, 2015 at 15:26

1 Answer 1

1

Using $http.post will not magically update your file. It will make a POST request to a given address. It means, that you need to have a server, that will handle your request.

Consider, that browsers are not allowed to manipulate files on your disk.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.