簡體   English   中英

從angularjs中的嵌套成功函數返回值

[英]Return a value from a nested success function in angularjs

我正在嘗試從代碼上的嵌套函數返回一個值,但是每當我嘗試顯示它時,它都會返回未定義的值。 這是我的代碼:

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);

                        });

在控制台中返回值之后。 它使我不確定。 最好的方法是什么?

您忘記了返回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");
            }
       });                 
}

您的代碼中有兩個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;


}

當外部調用完成時,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的解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM