问题
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