javascript - $scope.showDefaultState` 计算结果为未定义

标签 javascript angularjs

尽管在 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/

相关文章:

javascript - 在光滑的轮播中为视频添加自动播放

javascript - 在 Rails i18n 中插入 javascript var

javascript - 防止浏览器重新定位通过制表符导航到的焦点元素

AngularJs routeProvider错误

angularjs - 使用 Travis CI 测试使用 SASS 的 AngularJS 应用程序

javascript - WebStorm、Karma 和 Angular。如何调试 Angular 服务

javascript - 使用 Google Analytics 对子域进行跨域跟踪

javascript - Express session 不适用于 Firebase 托管

angularjs - 无法通过 Protractor JS 在 IE11 上运行多重兼容性测试

AngularJS:如何使用 ng-animate 在加载时为 ng-repeat 设置动画