jeudi 26 juin 2014

AgularJS Return Value From AJAX


Vote count:

0




Ok, so I have a service with a function that does an HTTP request and the value returned is set to $scope, however this does not work because it seems $scope is not waiting for the AJAX call. How would I go about returning an AJAX result from a service function outside the controller?


Example:



//Controller
app.controller('Controller', function($scope, $http, Service){

// $scope WILL NOT BE SET TO THE VALUE RETURNED, the returned value is UNDEFINED
$scope = Service.getData();

}


// Get server data from service function
app.service('Service', function ($http){

this.getData = function(){
//Get server data

return { data: result }
}
}


asked 28 secs ago






Aucun commentaire:

Enregistrer un commentaire