javascript - 通过ajax加载图像时出现无限循环

标签 javascript ajax angularjs node.js

我试图在 node.js 中将用户上传的文件设为私有(private)。我正在使用 angular 在客户端提供文件。

<div ng-repeat="message in messages">
    <div class="details" ng-if="message.type == 'photo'">
    <img ng-src="{{ chat_ctrl.getPhoto(message.photo, message.conversation_id, message.type) }}"  class="message-photo" alt="{{ message.text }}">
    <p>{{ message.text }}</p>
    <small am-time-ago="message.timestamp" am-preprocess="unix"></small>
    </div>
</div>

这是我用来获取文件的函数。

    me.getPhoto = function(url, id, message_type){

        if(message_type == 'photo'){            
            RequestsService.getPhoto(url, id).then(function(response){
               console.log(response); //returns the data-uri of the image
               return response;
            });
        }else{
            return '';
        }

    };

该函数从向 node.js 服务器发出请求的 RequestService 调用 getPhoto 方法:

    function getPhoto(url, id){

        var deferred = $q.defer();

        var request = {
            method: 'GET',
            url: base_url + '/uploads' + url,
            params: {'url': url, 'id' : id}
        };

        $http(request)
            .success(function(response){
                $timeout.cancel(me.timer);
                deferred.resolve(response);
            })
            .error(function(data){
                deferred.reject();  
            });

        me.requestTimeout(deferred);

        return deferred.promise;        

    };

这是我遇到的错误:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []

服务器返回表示图像的数据 uri。我已经尝试使用 ng-click 调用相同的方法并返回数据 uri:

<img src="" ng-click="chat_ctrl.getPhoto(message.photo, message.conversation_id, message.type)"  class="message-photo" alt="{{ message.text }}">

有什么想法吗?

更新

我尝试直接返回从 http 请求中获取的数据,但我仍然遇到同样的错误。

$http(request)
            .success(function(response){
                //$timeout.cancel(me.timer);
                //deferred.resolve(response);
                return response;
            })
            .error(function(data){
                return data;
                //deferred.reject();    
            });

最佳答案

避免在 ng-src 中使用异步函数调用。 ng-src 中的表达式在每个 digest 上执行。

首先尝试在 Controller 中加载所有照片:

loadPhotos();

loadPhotos = function() {
    for(var i=0,max=messages.length; i<max; i++) {
        me.getPhoto(i);
    }
};

getPhoto = function(index){
    if(messages[index].message_type == 'photo'){            
        RequestsService
            .getPhoto(messages[index].photo, messages[index].conversation_id)
            .then(function(response){
                messages[index].photoSrc = response;
            });
    }
};

在您的模板中使用:

<img ng-src="{{message.photoSrc}}" ... />

关于javascript - 通过ajax加载图像时出现无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29598939/

相关文章:

javascript - 单击下拉菜单上的元素时,如何将焦点切换到输入文本?

javascript - 在 JavaScript 中创建 'X'

javascript - XMLHttpRequest 对象的 open() 和 send() 方法有什么区别?

php - 根据字段值传递数据库字段 ID 的运行 SQL

javascript - JqG​​rid 格式化程序按钮(操作)不可见

javascript - 如何在 Angular 应用程序中使用 OrientDB HTTP API?

angularjs - Angular.js 表达式抛出异常

javascript - Protractor 无法点击 <a> 元素内的嵌入 <span>

javascript - 使用 Phaser 框架将 Javascript 值发送到 PHP

javascript - 如何在Angular表分页中获取增量序列号