为什么$scope.showDefaultState
评估为未定义,尽管在if语句中设置了?
如何让它不评估为未定义但捕获" true"值?
// 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
}
答案 0 :(得分:4)
您可以将承诺结合起来并一次评估所有承诺。 $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
});