我想问一下then()
可以用在任何函数上吗?
在我的 Angular 应用程序中,我在尝试使用 then
时遇到错误(“无法读取未定义的属性 then”)。
例如我有这个功能:
self.getCommentsData = function() {
commentsService.getComments($routeParams.id)
.then(function (data){
//Do some stuff and at the end push to a scope array
$scope.commentsList.push(someValue);
});
}
稍后我想调用此方法,然后(并且仅此后)在完成后调用另一行代码,这是我正在使用 then
:
self.getCommentsData()
.then(function(){
$location.hash('goTotrue');
$anchorScroll();
});
这给了我一个错误 - 想知道我做错了什么?
谢谢
最佳答案
你应该在链函数中返回 promise
self.getCommentsData = function() {
return commentsService.getComments($routeParams.id)
.then(function (data){
//Do some stuff and at the end push to a scope array
$scope.commentsList.push(someValue);
});
}
关于javascript - JS Promises - Angular - 然后可以用于任何函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37141926/