0

I'm new to angular js. Here i have the code: I receive the response data like number. In this code how i assign the response data as $scope.vote_counting. In this code does not return anything.

$scope.votes = function(){
        var votes = $http({
              method: "post",
              url: "/getVotes",
              data: { id: $scope.Id}
            }).success(function(response){  
            });
          return votes;
    }

Please anyone help to this.

4 Answers 4

3

Simply call the $http. It does not have to be in a function

$http({
    method: "post",
    url: "/getVotes",
    data: { id: $scope.Id }
}).then(function(response) {
    //handle success
    $scope.votes_counting = response.data;
}, function(error){
    //handle error
});

The sort version is

$http.post("/getVotes", { id: $scope.Id }).then(function(response) {
    //handle success
    $scope.votes_counting = response.data;
}, function(error) {
    //handle error
})

Note : You are using a POST method but a GET method seems more appropriate in your case (getVotes)

Sign up to request clarification or add additional context in comments.

Comments

1

I've added a snippet, which shows the basic handling of promises. Here, I've used a service to mock a http call. The response is attached to a scope variable, which is presented in the view.

angular.module('TestApp', [])
  .factory('MockHttp', function($q) {
    return {
      getMockData: function() {
        return $q.when(['A', 'B', 'C']);
      }
    };
  })
  .controller('TestController', function($scope, MockHttp) {
    $scope.res = null;

    MockHttp.getMockData()
      .then(function(res)  {
        $scope.res = res;
      })
      .catch(function(err) {
        console.log(err);
      });

  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="TestApp">
  <div ng-controller="TestController">
    {{res}}
  </div>
</div>

Comments

0

The $http function does not return the response of the server. But as you have already figured out you can use the success function to get the servers response. Simply set $scope.votes value in the success function like this:

 $http({
   method: "post",
   url: "/getVotes",
   data: { id: $scope.Id}
 }).success(function(response){  
   $scope.votes = response
 });

Comments

0

The easiest is probably using $http.post. Note that success is deprecated in favour of then:

$scope.retrieveVotes = function(){
  $http.post('/getVotes', {id : $scope.id}).then(function(response){  
    $scope.votes = response.data;
  });
}

Also note that $http calls are asynchronous so calling retrieveVotes is also asynchronous.

3 Comments

Why $http.post() ?
@Weedoze Because it's a shortcut for $http({method: 'POST', ..) making your code more readable and concise
Oh yeah, I didn't see that he was using a post... Even if the url is getVotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.