angularjs - Using Resources to store data in json file in Angular -
folks have form on website who's data want store in json file.
here code form:
<form> <input ng-model="obj.firstname"> <input ng-model="obj.lastname"> <button ng-click="storedata()">click here store data</button> </form>
my angular code 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 data not stored. missing here ? here plunkr : http://plnkr.co/edit/gist:3662702
ngresource angular service designed interact restful server side data sources (see ngresource docs). angular js tutorials tend reference local json files since they're meant stand-alone examples can download , run locally without need back-end server. you'll notice tutorials read data json files, cannot update them.
if you're looking save data client side, check out localstorage (http://diveintohtml5.info/storage.html).
if you're trying save data server side, you'll need setup end service (via nodejs, php, .net, ruby, python, , many other frameworks..)
Comments
Post a Comment