在打字稿

时间:2015-07-15 02:13:00

标签: angularjs typescript

我在我的打字稿控制器中使用$ q.defer时遇到问题。我得到的错误就是这个。$ q.defer不是一个函数。这是我的打字稿......

export class AccountWizardController implements IAccountWizardScope {

  account: Account;

  static $inject = ['$q']
  constructor(private $q: ng.IQService) {
  }

  saveState(): ng.IPromise<Account> {
    console.log(this.$q);
    var deferred = this.$q.defer<Account>();
    console.log(deferred);
    return deferred.promise;
  }
}

export interface IAccountWizardScope {
  account: ContractMonitor.Models.Account;
  saveState(): ng.IPromise<Account>;
}

当我在视图中调用saveState()方法时,我得到了错误。这让我感到困惑。

感谢。

1 个答案:

答案 0 :(得分:0)

$q始终有deferref)。所以我的猜测是this.$qundefined,因为你的错误this就是这种情况。修复,使用胖箭头保留上下文:

saveState = (): ng.IPromise<Account> => {
    console.log(this.$q);
    var deferred = this.$q.defer<Account>();
    console.log(deferred);
    return deferred.promise;
}
相关问题