0

I want to get the data outside the function.

Here is my controller code:

 vm.someVar = "";
 selfprofileSrvc.GetPrfInfo(memId,function (response, status) {

    vm.someVar = response;
    alert(vm.someVar);
  });

Service:

Service:

this.GetPrfInfo = function (memberId, funCallBack) {
    $http({
        method: "GET",
        url: "http://my.com/api/profile/GetProfileInfo/" + memberId,
    }).success(function (response, status) {
        funCallBack(response, status);
    }).error(function (reason, status) {
        alert("Something went wrong!");
    });
}

In the above code I am getting data when the alert is in function but outside the data is empty.

halfer
  • 19,824
  • 17
  • 99
  • 186
anub
  • 527
  • 1
  • 4
  • 21
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Yury Tarabanko Mar 31 '17 at 08:54
  • Thats because you are trying to call the variable before the callback returns .Since javascript is asynchronous, outside code gets excecuted before the callback returns – Muhammed Neswine Mar 31 '17 at 08:56

0 Answers0