Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Folks I have a form on my website who's data I want to store in a json file.

Here is the code for the form:

 <form>
    <input ng-model="obj.firstname">
    <input ng-model="obj.lastname">     
    <button ng-click="storedata()">Click here to store data</button>        
</form>

My Angular code is as below:

var myApp = angular.module('app', ['ui.bootstrap.dialog','ngResource']);

myApp.controller('TestCtrl', function($scope,$dialog,TestResource) { 
 $scope.obj = {};
 $scope.obj.firstname = "Mahatma";
 $scope.obj.lastname = "Gandhi";

 $scope.storedata = function() {
    console.log("Storing Data now");
    TestResource.save($scope.obj);
    console.log("Data should have been stored");
 }
});


myApp.factory('TestResource', ['$resource', function($resource) {
  return $resource('test.json', {}, {} );
}]);

The problem is that the data does not get stored. Am I missing something here ? Here is a plunkr : http://plnkr.co/edit/gist:3662702

share|improve this question
 
What does store mean? Storing on server? Did you mean request is going? –  Chandermani Aug 26 at 13:36
 
nopes..just storing in a file.. I have a file in the same folder called test.json –  James Hans Aug 26 at 13:47
 
AngularJS or as a matter of fact, javascript can only send data to server, saving logic should be on server. –  Chandermani Aug 26 at 13:49
add comment

2 Answers

up vote 0 down vote accepted

ngResource is an Angular service designed to interact with RESTful server side data sources (see ngResource docs). The angular js tutorials tend to reference local JSON files since they're meant to be stand-alone examples which anyone can download and run locally without the need for a back-end server. But you'll notice the tutorials only read data from the JSON files, they cannot update them.

If you're looking to save data client side, check out LocalStorage (http://diveintohtml5.info/storage.html).

If you're trying to save the data server side, you'll need to setup some back end service (via NodeJS, PHP, .NET, Ruby, Python, and many other frameworks..)

share|improve this answer
add comment

Try $save instead of save like;

TestResource.$save($scope.obj);

Hope it helps.

share|improve this answer
 
I tried that already and it does not work. –  James Hans Aug 26 at 14:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.