0
爲什麼$scope.showDefaultState
評估爲未定義,儘管在if語句中設置?
我該如何獲得它未評估爲未定義但捕獲「真實」的價值?
// Determine if user has signed up in past
$cordovaNativeStorage.getItem("userPicture").then(function (value) {
$scope.userInfo.userPicture = value;
}, function (error) {
$log.log(error);
});
$cordovaNativeStorage.getItem("userProfile").then(function (value) {
$scope.userInfo.userProfile = value;
}, function (error) {
$log.log(error);
});
$cordovaNativeStorage.getItem("userSelectedCategories").then(function (value) {
$scope.userInfo.userSelectedCategories = value;
}, function (error) {
$log.log(error);
});
if($scope.userInfo.userPicture != null && $scope.userInfo.userProfile != null && $scope.userInfo.userSelectedCategories != null) {
$scope.showDefaultState = true;
}
console.log($scope.showDefaultState);
if($scope.showDefaultState == null) {
// stuff goes here... the code in this block always runs
}
它可能爲空,因爲在執行設置showDefaultState = true的if條件時,promise沒有解決。嘗試給超時值,然後看看它是否有幫助? –
感謝您回覆我。我不想使用超時,有沒有其他的選擇? – methuselah
@clever_bassi不,這不是一個等待承諾解決的好方法。你需要將你的函數鏈接成類似'Promise.all'的東西,然後在解析時,你的'showDefaultState'被設置,或者爲null,你可以繼續。 –