Angular,将JSON对象推送到服务器

时间:2016-08-20 01:20:07

标签: angularjs json angular-resource json-server

我在db.json文件中使用json-server和db.json我有一个当前为空的数组"feedback":[],其中用户应该能够从应用程序发送反馈。

但没有任何东西被推入服务器,get方法有效(从服务器获取GET)但没有PUT

这是我的服务:

angular.module('confusionApp')
.constant("baseURL", "http://localhost:3000/")
.service('feedbackService',['$resource','baseURL',function($resource,baseURL){
      this.getFeedback=function(){
        return $resource(baseURL+"feedback/",null,{
          'update':{
            method:'PUT'
          }
        });
      };
    }]);

这是控制器:contactus.html包含一个反馈表单,因此有两个控制器

// contactus.html controllers
.controller('ContactController', ['$scope', function($scope) {
        $scope.feedback = {
            firstName: "",
            lastName: "",
            email: "",
            date:""
        };
    }])
    // Feedback form controller
    .controller('FeedbackController', ['$scope','feedbackService', function($scope,feedbackService) {
$scope.feedbacks=feedbackService.getFeedback().query();
        $scope.sendFeedback = function() {

                $scope.feedback.date=new Date().toISOString();
                $scope.feedbacks.push($scope.feedback);
                $scope.feedbackForm.$setPristine();
                $scope.feedback = {
                    firstName: "",
                    lastName: "",
                    email: "",
                    date:""
                };
        };
    }]) 

2 个答案:

答案 0 :(得分:1)

推送新反馈后,调用update方法更新数据

var feedbackService = feedbackService.getFeedback();
...

$scope.feedbacks.push($scope.feedback); 
feedbackService.update($scope.feedbacks)

答案 1 :(得分:0)

在服务中使用POST和POT方法之后,我意识到服务器中的任何更改都不需要改变控制器 the answer to the same question in different approach