Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular setting variable as object through http

I'm trying to set a variable as the data object returned from a http request in angular, but the variable never sets to even if it is in the $scope unless it is nested within the success function. For example, if I do this in the controller :

    $scope.hello = [];
       var getAppointmentsurl = './dbscripts/getAppointments.php';

          $http({method: 'GET', url: getAppointmentsurl}).success(function(data) {

                $scope.hello = data;
          });

          console.log($scope.hello);

}

Hello is blank... so I set it up in services.js like this :

 this.getCalendarData=function(){
        var hello = [];
           var getAppointmentsurl = './dbscripts/getAppointments.php';

              $http({method: 'GET', url: getAppointmentsurl}).success(function(data) {

                    hello = data;
              });

              return hello;

    }

but still hello is blank. Am I missing something obvious?

edit -- enter image description here

like image 541
Gaz Smith Avatar asked Feb 03 '26 02:02

Gaz Smith


1 Answers

 this.getCalendarData=function(){
       var getAppointmentsurl = './dbscripts/getAppointments.php';

          return $http({method: 'GET', url: getAppointmentsurl}).success(function(data) {

                return data;
          });


}

This is asynchronus call we have to return data like above.

like image 174
Akash Bhandwalkar Avatar answered Feb 05 '26 16:02

Akash Bhandwalkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!