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.