方法返回后,异步回调会触发(angularjs .then())

时间:2015-08-27 10:38:54

标签: angularjs asynchronous typescript

我有以下代码:(我尝试了很多方法)

performLogin = (data: LoginRequest) => {
  var deferred = this.qService.defer();
  var response = this.httpService.get(
  myServiceURL + '&' + ObjecttoParams(data))
  .then(function (response) {
    debugger;
    this.loginResponse = JSON.parse(JSON.stringify(response.data));
  });
  debugger;
  deferred.resolve(response);
  debugger;
  return(this.loginResponse);
}

这是我服务的构造函数:

  constructor($http: ng.IHttpService, $location: ng.ILocationService, $q: ng.IQService) {
            this.qService = $q;
            this.httpService = $http;
            this.locationService = $location;
            this.loginResponse = new LoginResponse();
            this.serviceURL = staticURL + currentServiceName;

        }

我正在调用我的控制器中的performLogin,他希望收到一个“LoginResponse”类,但因为.then()稍后发生,我的“返回this.loginResponse”发生在.then()之前调用。

如何让他在返回之前等待.then()完成?

0 个答案:

没有答案