Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My template can be found here http://goo.gl/xEttgl. I am hardcoding postData by declaring it as a global variable as

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;
     });
  });

but what i would like to do is send postData dynamically from $scope inside StartUpController i.e lets say i click upvote button and i want to send the current value of $scope.upvotes in POST and display it in the result.

share|improve this question
add comment

1 Answer

I over complicated the problem by using a factory, I found a simple solution, instead of using factory for POST i can directly send an $http.post request from the Controller. A sample solution is as follows

$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;

     });
     };
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.