【问题标题】:Return a value from a nested success function in angularjs从angularjs中的嵌套成功函数返回一个值
【发布时间】:2016-08-29 07:06:42
【问题描述】:

我试图从我的代码中的嵌套函数返回一个值,但每当我尝试显示它时它都会返回未定义的值。这是我的代码:

var calculateAlbum = function (albumPath,albumName) {
            return photoService.getPhotos(albumPath,albumName)
                    .success(function (data){                       
                        var status = data.myphotos.status;
                            if(status == "ok"){
                                    photoService.myphotosPhotoList()
                                        .success(function(data){
                                            $scope.allPhotoListTotal = {};
                                            var getAllPhotoList = data.config.nas_sharing;
                                            $scope.allPhotoListTotal = getAllPhotoList.count;
                                        });
                            }
                    });                 
    }

返回一个值:

calculateAlbum(albumPath,fileName).then(function(data) {            
                            var gettotal = $scope.allPhotoListTotal;
                            console.log(gettotal);

                        });

在控制台中返回值后。它发送给我未定义。最好的方法是什么?

【问题讨论】:

  • 你需要阅读 Angular 中的 Promise。最有可能发生的是您在异步请求解决之前尝试使用一个值。基本上,你是想在得到报酬之前花钱。

标签: javascript angularjs function nested-function


【解决方案1】:

您的代码中有两个 Promise,但您只是链接了第一个(外部)Promise。您的内在承诺设置了您想要的值,因此您需要等待您的内在承诺解决才能使用它。

一种方法是注入承诺服务$q,然后返回您自己的承诺对象。

var calculateAlbum = function (albumPath,albumName) {
        var deferred = $q.defer();
        photoService.getPhotos(albumPath,albumName)
                .success(function (data){                       
                    var status = data.myphotos.status;
                        if(status == "ok"){
                                photoService.myphotosPhotoList()
                                    .success(function(data){
                                        var getAllPhotoList = data.config.nas_sharing;
                                        $scope.allPhotoListTotal = getAllPhotoList.count;
                                        deferred.resolve(data)
                                    });
                        }
                });
        return deferred.promise;


}

【讨论】:

    【解决方案2】:

    calculateAlbum 返回一个promise,它在外部调用完成时完成,但它不考虑内部执行。您需要在内部成功函数中返回 promise 以等待另一个异步方法。在这里,我们利用$q 的力量来做到这一点。

    var calculateAlbum = function (albumPath,albumName) {
            return photoService.getPhotos(albumPath,albumName)
                    .success(function (data){                       
                        var status = data.myphotos.status;
                        var defer = $q.defer();
                            if(status == "ok"){
                                    photoService.myphotosPhotoList()
                                        .success(function(data){
                                            $scope.allPhotoListTotal = {};
                                            var getAllPhotoList = data.config.nas_sharing;
                                            $scope.allPhotoListTotal = getAllPhotoList.count;
                                            defer.resolve(); //Async execution end here
                                        })
                                        .error(defer.reject);
                            } 
                            else
                               defer.reject();
                         return defer.promise;
                    });                 
    }
    

    注意:这是一种反模式,但没有其他办法,因为 photoService.myphotosPhotoList() 只在某些时候执行。

    编辑:实际上还有其他方法。看看icycool的解决方案

    【讨论】:

    • 您可以返回内部承诺,这样外部承诺只会在内部承诺解决时才解决:example
    • 我知道,但我不知道你可以在没有延迟实例的情况下使用$q.reject/resolve
    【解决方案3】:

    你忘了回复photoService.myphotosPhotoList的承诺。

    当状态不正确时,您也忘记拒绝第一个承诺(您可以测试这种情况)。

    function calculateAlbum (albumPath,albumName) {
        return photoService.getPhotos(albumPath,albumName)
            .then(function (data){                       
                var status = data.myphotos.status;
    
                if(status == "ok") {
                    return photoService.myphotosPhotoList()
                        .then(function(data){
                            $scope.allPhotoListTotal = {};
                            var getAllPhotoList = data.config.nas_sharing;
                            $scope.allPhotoListTotal = getAllPhotoList.count;
                        });
                }
                else {
                    return $q.reject("status not ok");
                }
           });                 
    }
    

    【讨论】:

    • 当我将成功更改为 then.. 状态变得未定义,即使我曾经定义它。所以我把它带回了成功,但仍然导致我不确定。
    • @Amboom 在谷歌中长时间搜索后,我找不到使用.success 的示例,它不是来自$http 服务。你能修改这个fiddle,让它使用.success而不是.then吗?或者更好的是,您可以更改您的函数,使其改用.then
    • 我刚刚使用了自定义服务来退还我的 $http:photoService.myphotosPhotoList().. 但无论如何,我真的很感谢您为帮助我所做的努力.. 在您的所有帮助下,它现在正在运行.. 谢谢跨度>
    • 不客气。虽然解决别人的问题很满足,但如果我能同时给你展示最佳实践,那就更棒了。在角度世界中使用.then 而不是.success 是更常见的做法(您可以在google 中搜索then vs success)。如有任何与 Promise 相关的问题,请随时提出。
    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2018-07-16
    • 2011-03-23
    相关资源
    最近更新 更多