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,你可以继续。 –