如何获得功能以外的响应?

时间:2017-03-31 08:53:07

标签: angularjs

我想在函数外部获取数据。

这是我的控制器代码:

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

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

服务:

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!");
    });
}

在上面的代码中,当警报处于运行状态但数据外部为空时,我将获取数据。

1 个答案:

答案 0 :(得分:0)

这是因为您试图在函数返回之前调用vm.someVar。由于selfprofileSrvc.GetPrfInfo是一个异步调用,所以它无法获取值,它只在异步调用后获取值。

因此,只有在vm.someVar调用完成后才尝试使用asynchronous

相关问题