繁体   English   中英

从angularjs的$ scope发送$ http.post中的动态数据

[英]Send dynamic data in $http.post from $scope in angularjs

我的模板可以在http://goo.gl/xEttgl中找到。 我通过将其声明为全局变量来对postData进行硬编码

var postData = {'result': '27'};
app.factory('mypostService', function($http) {
return {
 postFooOldSchool: function() {
   $http.post('/angularresult', JSON.stringify(postData)
   ).success(function(data, status, headers){

   }).error(function(data, status, headers){

   });
  }
 }
});
app.controller('StartUpController', function($scope, $http, myService, mypostService) {
     $scope.upvotes = 0;
     $scope.serverupvotes = 0;
     $scope.increase = function(){
             $scope.upvotes = $scope.upvotes + 1;
     };
     mypostService.postFooOldSchool(function(data) {

     });
     myService.getFooOldSchool(function(data) {
            $scope.result = data;
     });
  });

但我想做的是从StartUpController内的$ scope中动态发送postData,即可以说我单击了upvote按钮,我想在POST中发送$ scope.upvotes的当前值,并在结果中显示它。

我通过使用工厂使问题复杂化了,我找到了一个简单的解决方案,而不是将工厂用于POST,我可以直接从Controller发送$ http.post请求。 示例解决方案如下

$scope.increase = function(){
    $scope.upvotes = $scope.upvotes + 1;
            var x = $scope.upvotes;
            alert(x)
            var postObject = {'result': x};
            $http.post('/angularresult', JSON.stringify(postObject)).success(function(data){
            });
            myService.getFooOldSchool(function(data) {
               $scope.result1 = data;

     });
     };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM