0
  $scope.results = searchService.CallWeb($scope.userName);

I am using the above line of code inside my angularjs controller. The method searchService.CallWeb() is inside an angularjs service that makes a jquery ajax call. The Jquery ajax call is successfully executed (I am throwing the results into an alert inside jquery success ), but the service method searchService.CallWeb() returns before it can get a response from the ajax call. Hence I am not able to get the results back to my controller. How do I fix this?

developer747
  • 15,419
  • 26
  • 93
  • 147

1 Answers1

1

1) Send a callback function into the service and when the ajax call from jQuery is complete, call the callback. In the callback you send in, populate the array or call the alert or whatever you need to do.

searchService.CallWeb($scope.userName, function(data){
  $scope.results = data;
});

2) You can also use an Angular resource to do this. Replace the jQuery ajax call with a resource call and instead of returning the value from the service, return the promise that the resource provides. Then you can use the promise in your controller to take the data and do whatever with it.

BoxerBucks
  • 3,124
  • 2
  • 21
  • 26