我正在使用 ES6 和 babel 开发 Angular 全栈。
在我的 Controller 中,我有:
$onInit() {
this.$http.get('/api/example')
.then(() => {console.log("task1")})
.then(() => {console.log("task2")})
}
控制台结果是我想要的:
task1
task2
但是当我尝试重构我的代码时:
$onInit() {
this.$http.get('/api/example')
.then(() => {console.log("task1")})
.then(aFunction())
}
aFunction() {
console.log("task2")
}
控制台结果是:
task2
task1
为什么会这样?
注意:.then(() => {this.aFunction()});
似乎可行,但似乎不是一个干净的解决方案。
最佳答案
您应该传递像 .then(aFunction)
这样的函数引用,而不是函数调用。当前您正在执行的 aFunction()
会立即调用该函数。
$onInit() {
this.$http.get('/api/example')
.then(() => {console.log("task1")})
.then(aFunction)
}
关于javascript - 订单 promise 在 AngularJs 中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38832522/