-1

I am using angular.js for my project and i wrote one service in which I have return patient details but in controller i am not getting this return value by patientinfo() function call. here is my service code:

app.service('PatientDetailService', function() {

  this.patientinfo = function(){

    $.ajax({
    dataType: "json",
    url: "json/client_info.json",
    type: 'get',
    error: function(patientdetail){ alert("error");}, 
    success: function(patientdetail){
        alert("successs");
        console.log(patientdetail);

              return patientdetail;

          }
    });

    }
});

1 Answers1

0

It's an asynchronous call. You don't get the results until the AJAX call completes (at which point your function will be called as part of the event loop).

You won't get the reply during the execution of patientinfo – instead, you'll have to set up some other function to handle the results.

Jeremy Roman
  • 16,137
  • 1
  • 43
  • 44
  • :can you please explain me how to handle the result by some other function because i don't know how to handle this value and i am not able to get my result. – Mahip Soni Mar 11 '13 at 09:16
  • Instead returning a result from `patientinfo`, it should accept a function that you can call when the results are available. Looks for a tutorial that explains asynchronous programming in JavaScript (using callbacks). [This one](http://recurial.com/programming/understanding-callback-functions-in-javascript/) seems fine, but you may find another more helpful. – Jeremy Roman Mar 11 '13 at 09:26