【发布时间】:2015-12-09 10:44:16
【问题描述】:
我希望能够在 .then 范围内调用函数,为此我使用 this.foo() 方式。但是,如果我在 .then 中执行此操作,则会出现错误,因为 this 似乎丢失了。我能做什么?
在这段代码中,这相当于对象 this
具有相同的输出console.log(this)
one().then(function() {
console.log(this)
})
function one() {
var deferred = $q.defer();
deferred.resolve()
return deferred.promise;
}
这似乎都不起作用
console.log(this)
var a = this;
one().then(function(a) {
console.log(a)
})
【问题讨论】:
-
如果您从
one().then(function(a) {中删除a参数,使其成为one().then(function() {,那么这将为您提供您想要的结果。 -
是的,当我看到你的答案时,我正在测试它。你完全正确!如果您将其发布为答案,我会将其标记为答案
标签: javascript promise angular-promise