尽管在 if 语句中进行了设置,但为何 $scope.showDefaultState
的计算结果为未定义?
如何让它不评估为未定义但捕获“真实”值?
// 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
}
最佳答案
您可以合并 promise 并立即评估所有内容。 $q
var p1 = $cordovaNativeStorage.getItem("userPicture");
var p2 = $cordovaNativeStorage.getItem("userProfile");
var p3 = $cordovaNativeStorage.getItem("userSelectedCategories");
$q.all([p1, p2, p3]).then(function (data) {
$scope.userInfo.userPicture = data[0];
$scope.userInfo.userProfile = data[1]; ;
$scope.userInfo.userSelectedCategories = data[2];
if ($scope.userInfo.userPicture != null && $scope.userInfo.userProfile != null && $scope.userInfo.userSelectedCategories != null) {
$scope.showDefaultState = true;
}
}).catch (function (err) {
console.log(err.message); // some coding error in handling happened
});
关于javascript - $scope.showDefaultState` 计算结果为未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40965202/