我有以下代码:
private resetPassword() {
this.auth.subscribe(auth => {
this.firebaseRef.auth().sendPasswordResetEmail(auth.auth.email).then(
success => {
this.successMessage = 'A password reset email was sent to ' + auth.auth.email + '. Please follow' +
'the instructions in the email to reset your password.';
},
error => {
console.log(error);
}
)
});
}
我遇到的问题是this.successMessage
没有成功。我可以在那里放console.log
并且它有效,但似乎我无法在该范围内引用this
。我尽可能地尝试使用.bind(this)
,但它没有帮助。有什么想法吗?
请注意,这是一个Angular2应用,并且successMessage
正在模板中呈现,因此我知道它不起作用。我已经确认,如果我在then
之外设置消息它可以正常工作。
答案 0 :(得分:1)
感谢@ AR7,我找到了解决方案。 this.successMessage
正在正确更新,但Angular2模板未更新。我通过将this.successMessage
作业包装在ngZone.run
:
constructor(private ngZone: NgZone) {}
...
private resetPassword() {
this.auth.subscribe(auth => {
this.firebaseRef.auth().sendPasswordResetEmail(auth.auth.email).then(
success => {
this.ngZone.run(() => {
this.successMessage = 'A password reset email was sent to ' + auth.auth.email + '. Please follow' +
'the instructions in the email to reset your password.';
});
console.log(this);
},
error => {
console.log(error);
}
)
});
}