Pares
Pares

Reputation: 57

How to use scope variable from http.get outside the success function in angular?

I would like to know how can I use the scope.hello value outside the success function in my controller. Here is where its getting populated in my controller file:

$http.get(BaseUrl().url + 'jury/UpcomingEvents?someID=' + someID)
     .success(function (response) {
         $scope.UpcomingEvents = response;
         $scope.hello= $scope.UpcomingEvents[0].EventID;
     });

I want to be able to use the value of $scope.hello in another array outside the http.get function like so:

var Count=3;
$scope.Total= $scope.hello + Count

so that I can output $scope.Total in my html file as following.

{{Total}}

Right now, $scope.hello remains undefined as its not retaining the value from the success function as I understand, it is an asynchronous function.

Is there a way to use this value in other places in my controller?

Thanks.

Upvotes: 1

Views: 422

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

Why cant you just do this,

var count = 3;
$http.get(BaseUrl().url + 'jury/UpcomingEvents?someID=' + someID)
     .success(function (response) {
         $scope.UpcomingEvents = response;
         $scope.hello= $scope.UpcomingEvents[0].EventID;
         $scope.Total= $scope.hello + Count
});

Upvotes: 1

Related Questions