How to push an object into local .json in Angular?

吃可爱长大的小学妹 提交于 2019-12-11 09:59:59

问题


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?

来源:https://stackoverflow.com/questions/24025155/how-to-push-an-object-into-local-json-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!