【问题标题】:Angular, push JSON object to serverAngular,将 JSON 对象推送到服务器
【发布时间】:2016-12-27 05:42:48
【问题描述】:

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

但没有任何东西被推送到服务器,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:""
                };
        };
    }]) 

【问题讨论】:

    标签: angularjs json angular-resource json-server


    【解决方案1】:

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

    var feedbackService = feedbackService.getFeedback();
    ...
    
    $scope.feedbacks.push($scope.feedback); 
    feedbackService.update($scope.feedbacks)
    

    【讨论】:

      【解决方案2】:

      在服务中使用 POST 和 POT 方法后,我意识到不需要对服务进行任何更改,只需对控制器进行一点更改 the answer to the same question in different approach

      【讨论】:

        猜你喜欢
        • 2018-09-24
        • 1970-01-01
        • 1970-01-01
        • 2011-09-15
        • 2016-12-15
        • 2016-01-30
        • 1970-01-01
        • 1970-01-01
        • 2019-02-21
        相关资源
        最近更新 更多