Angularjs 在typescript中使用defer
我在typescript控制器中使用$q.defer时遇到问题。我得到的错误是,$q.defer不是一个函数。这是我的打字稿Angularjs 在typescript中使用defer,angularjs,typescript,Angularjs,Typescript,我在typescript控制器中使用$q.defer时遇到问题。我得到的错误是,$q.defer不是一个函数。这是我的打字稿 export class AccountWizardController implements IAccountWizardScope { account: Account; static $inject = ['$q'] constructor(private $q: ng.IQService) { } saveState(): ng.IPro
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>;
}
导出类AccountWizardController实现IAccountWizardScope{
账户:账户;
静态$inject=['$q']
构造函数(私有$q:ng.IQService){
}
saveState():ng.IPromise{
console.log(这是$q);
var deferred=这个$q.deferred();
控制台日志(延迟);
回报。承诺;
}
}
导出接口IAccountWizardScope{
账户:ContractMonitor.Models.account;
saveState():ng.IPromise;
}
当我在视图中调用saveState()方法时,我得到了错误。这让我完全困惑不解
谢谢。
一直以来都有延迟
)。所以我猜,this.$q
是未定义的
,这是因为您的this
错误。若要修复此问题,请使用胖箭头保留上下文:
saveState = (): ng.IPromise<Account> => {
console.log(this.$q);
var deferred = this.$q.defer<Account>();
console.log(deferred);
return deferred.promise;
}
saveState=():ng.IPromise=>{
console.log(这是$q);
var deferred=这个$q.deferred();
控制台日志(延迟);
回报。承诺;
}
但是当我在this.$q上执行console.log操作时,我会将Account对象取回。我试着加上胖箭,但没有用。我已将界面中的签名更改为:saveState:()=>ng.IPromise
但我仍然得到错误。$q
是帐户
对象吗?是。在控制台中,我从console.logIt中看到对象{account:Object},似乎在缩小this.q.defer后,this.q.defer变为c.$q.defer。缩小后我如何保持t为这个值。这个错误是如何产生的?它是在浏览器中运行,还是在运行单元测试?可以包含呈现的JavaScript吗?