作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我被困在为什么我的 promise 不起作用。从我看过的其他文章来看,我认为我做得对。这是我目前的代码:
工厂代码
factory.isLoggedIn = function() {
$http.get('/api/v1/fitbit/auth')
.success((data) => {
return data.status;
})
.error((error) => {
console.log('Error: ' + error);
});
}
$scope.fitbitAuth = function() {
FitbitFactory.isLoggedIn()
.then(
function(data) {
$scope.fitbitStatus = data;
},
function(errorData) {
console.log(errorData);
});
return $scope.fitbitStatus;
};
return $scope.fitbitStatus
应该填充
$scope.fitbitAuth
,但事实并非如此。我还在工厂中返回一个 bool 值,它应该填充
$scope.fitbitStatus
.
最佳答案
您必须 return
某事( promise ),或者是undefined
.
工厂代码:
factory.isLoggedIn = function() {
return $http.get('/api/v1/fitbit/auth');
}
$scope.fitbitAuth = function() {
return FitbitFactory.isLoggedIn()
.then(
function(data) {
$scope.fitbitStatus = data;
},
function(errorData) {
console.log(errorData);
});
};
return $scope.fitbitStatus;
因为它在返回时是未定义的。
关于javascript - Angular "Cannot read property ' then' of undefined"with promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40811815/
我是一名优秀的程序员,十分优秀!