Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to use $http to grab binary images from the service I am consuming, but I think I am hitting an asynchronous issue.

Here is the code I am using in my controller:

$scope.downloadImage = function(imgReady, index) {
    if (imgReady == false) {

        for (var i = $scope.vehicles[index].events.length - 1; i >= 0; i--) {
            var config = {
                method: 'POST',
                url: '/Events/SnapShot',
                data: $scope.vehicles[index].events[i],
                cache: false
            }

            RequestService.makeApiRequest(config).success(function(response) {
                console.log(response.data); // shows the binary data
                $scope.vehicles[index].events[i].snapshot = response.data; // Results in: TypeError: Cannot set property 'snapshot' of undefined
            });

            console.log($scope.vehicles[index].events[i]); // Logs event object without snapshot property
        };
    }
}

Here is the code in my view:

<li ng-repeat="vehicle in vehicles" ng-init="isHidden=false; imgReady=false;" class="event-list-animation">
    <div ng-click="isHidden=!isHidden; downloadImage(imgReady, $index); imgReady=true" class="heading" ng-hide="vehicle.events.length < 1">
        <h1>Vehicle: {{ vehicle.name }}</h1>

        <span ng-class="(isHidden == false) ? 'details-toggle' : 'details-toggle open'">
            expand / collapse
        </span>
    </div>
    <div ng-show="isHidden" class="body event-show-hide-animation">
        <div class="wrap" ng-repeat="event in vehicle.events">
            <ul>
                <li>Event: {{ event.number }}</li>
            </ul>

            <img ng-src="{{ event.snapshot }}" />
        </div>
    </div>
</li>

So I have several vehicles, with events under each vehicle, I am trying to grab the image for each event. But it appears to be causing an asynchronous issue. I've also tried:

$scope.vehicles[index].events[i].snapshot = RequestService.makeApiRequest(config).success(function(response) {
    return response.data;
});

However this seems to result in the following issue: GET http://localhost:3000/%7B%7D 404 (Not Found)

Is this the binary image data attempting to load? This is the first time I've worked with binary image data, and I have been told to use ng-src directive to make it load correctly.

Any assistance on this issue is appreciated, thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I think the TypeError because you wrongly capture the i in the function closure.

Please try this:

RequestService.makeApiRequest(config).success(function(j) {
    return function(response) {
        console.log(response.data);
        $scope.vehicles[index].events[j].snapshot = response.data;
    }
}(i));
share|improve this answer
    
This is producing the same issue as the second attempt, so it looks as though its a further issue loading the data into the front-end :S Thanks –  Neil Dec 5 '13 at 16:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.