我被困在爲什麼我的承諾沒有工作。從我看過的其他文章中,我認爲我做得正確。下面是代碼我目前:角度「無法讀取屬性」,然後「未定義」,承諾
廠代碼
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
的理解,但它不是。我也在工廠返回了一個布爾值,它應該填充爲$scope.fitbitStatus
。
在factory.isLoggedIn – vijay
的$ http之前添加return語句並擺脫工廠中的成功和錯誤。 – fubbe