【问题标题】:Page load happens before data returned via xhr in angular页面加载发生在通过 xhr 以角度返回的数据之前
【发布时间】:2016-12-11 19:29:30
【问题描述】:

我们在加载从 S3 获取的文档 url 并在仪表板标签页上显示缩略图时面临挑战。

  /***fetch docs **push success confirm***/
    $scope.getdocuments = function(){
        DDListDataService.getDocumentTypes().then(function(docs){
            $scope.docTypes = docs.data;
        });

        userService.fetchUserDocs($scope.user.customerId,$scope.user.entityType).then(function(userDocs){ 

            //console.log("result--"+userDocs);

            angular.forEach(userDocs, function(object , key){
                console.log(object);
                var xhr = new XMLHttpRequest();
                var blob;
                xhr.open('GET', object.documents[0].documentUrl);
                //var file = common.base64ToBlob(object.d, true);
                xhr.responseType = 'arraybuffer';

                xhr.onload = function(e) {

                    //var file = remoteToBlob(object.documents[0].documentUrl);
                    //var file = common.base64ToBlob(object.documents[0].document, 'image/jpg');
                    /*file.lastModifiedDate = new Date();
                    file.name = "";*/

                    var file = new Blob([this.response], {type: 'image/jpg'});
                    //rest of the code that uses the blob goes here

                    file.lastModifiedDate = new Date();
                    file.name = "";

                    //setTimeout(function(){console.log(file)},1000);

                    var options = {
                            customerId : $scope.user.customerId,
                            customerType : $scope.user.entityType,
                            docTypeName : object.key.documentName,
                            documentName : object.key.documentNameId,
                            documentVersion : object.key.documentVersion,
                            documentUID : object.doccumentUID,
                            //requestedBy : $scope.user.customerType,
                            requestedBy : LocalService.get('userType'),
                            requestedById : $scope.user.customerId

                    }

                    var fileItem = new FileUploader.FileItem($scope.uploader,file,options);
                    fileItem.progress = 100;
                    fileItem.isUploaded = true;
                    fileItem.isSuccess = true;

                    /*file.customerId = $scope.user.customerId;
                    file.customerType =$scope.user.entityType;
                    file.documentName = object.key.documentNameId;
                    file.documentVersion =object.key.documentVersion;
                    file.documentUID = object.doccumentUID;
                    file.requestedBy = $scope.user.customerId;
                    file.requestedById = $scope.user.customerId;*/

                    $scope.uploader.queue.push(fileItem);
                    console.log(fileItem);
                };
                xhr.send();
            });
        });
        //$scope.uploader.formData=[{"user":angular.json($scope.user)}];
        $scope.uploader.queue.length = 0;
        }

xhr.send() 执行后,页面加载发生,但数据还没有准备好,它给出了未定义的文件对象,因为同时没有返回数据。在返回数据之前,我们如何才能拖拉负载?我认为 onload 有一个回调应该执行该功能。如果错误,请纠正我。
怎么处理?
显示图像的 html 是

   <div ng-show="uploader.isHTML5" ng-thumb="{ file: item._file, height: 100 ,stream:item._file}"></div>

【问题讨论】:

  • 你为什么不使用 angular ajax 语法?
  • Angular 的新手。您能解释一下它在 Angular 中的工作原理吗?它会解决我们面临的问题吗? @madalinivascu
  • 你应该使用angularsee an example here提供的$http服务。
  • @oliv37 您能否建议对当前代码进行更正?我想让当前的工作正常运行,然后为此编写服务。

标签: javascript jquery angularjs ajax xmlhttprequest


【解决方案1】:

页面加载所需的时间与您的 xhr 请求完成所需的时间无关。

因此,创建一个调用另一个函数的 window.load 函数,并将 xhr 请求之后所需的内容放入另一个函数中,如下所示:

$(window.load(function (){
afterXHRLoad(); //This calls the afterXHRLoad function after the page loads
});

function afterXHRLoad(){
if($.active == 0)
{
//code needed here;
}
else
{
setTimeout(afterXHRLoad, 1000); //recursively call the function after 1 second
}
}

【讨论】:

  • 感谢@Mr. 的回复。鲸鱼会尝试并告诉您。
猜你喜欢
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
相关资源
最近更新 更多