1

As the title suggests, how do I update my .json file after a user inputs stuff? I can do a .get and display the json contents but I'm lost on how to push/update my json.

Here's my factory service:

angular.module('services', [])
    .factory('localpolls', ['$http', function($http){
        return {
            get: function(callback){
                $http.get('polls/polls.json').success(function(data){
                    callback(data);
                });
            }
        };

    }]);

Here's the part of my controller for the view I'm working in:

angular.module('controllers')
        .controller('pollCtrl', ['$scope', 'localpolls', function ($scope, localpolls){

        localpolls.get(function(data){
            $scope.polls = data;
        });

$scope.question = {pollQuestion:''}
$scope.responses = [
        {
            response: ""
        },      
        {
            response: ""
        },      
        {
            response: ""
        },      
        {
            response: ""
        }];

$scope.submitPost = function (){
    if(confirm('Create Poll?')){
        $scope.polls.push($scope.question, $scope.responses);
        $scope.responses = [
        {response: ""},     
        {response: ""}, 
        {response: ""},     
        {response: ""}];
        $scope.question = {pollQuestion: ''};


    }else{
        return;
    };


};

You can see in my submit post I have the $scope.polls.push but this doesn't update the .json with the user input object. Am I doing this wrong? What do I need to do?

3
  • This can only be achieved via server side logic. You can create an API for that and call the API in AngularJS. Commented Jun 3, 2014 at 21:57
  • You mean with $http.post? I Feel like that's what i'm supposed to use but don't exactly know how. I know I probably should put it into my service but I don't know exactly how. btw the json file is a local .json file Commented Jun 3, 2014 at 22:05
  • you cant do that with angular,it's not its concern. Commented Jun 4, 2014 at 6:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.