Ok, so I have a simple angular app, on one controller i have som items bound to my $scope like so: $scope.items[{"name":"stuff,"image";"imageName"......}
The image property is a string that though a api can be resolved to a URL in a async http call. I need to bind the resolved url to the ng-src in a view.
The code to resolve the name to a url looks like this:
BaasBox.getImageURI("testimage", {'resizeId' : 1})
.done(function(res) {
console.log("image URI is ", res['data']);
})
.fail(function(error) {
console.log("error ", error);
})
My first thought was to call a function from ng-src on the image and then return the result but this can not be done because it's a async call. So my question is: How to i get the url the the view in a smooth way, could a directive or a filter be used?
Updated attempt, this actually works (almost... need to press fetch a few times before the image shows) but I'm sure that this is not the way to go, there is a simple way I'm sure.
controller:
.controller('PlaylistsCtrl', function ($scope) {
$scope.GetImage = function(i,name){
BaasBox.getImageURI(name, {'resizeId' : 1})
.done(function(res) {
$scope.playlists[i].img = res['data'];
})
.fail(function(error) {
console.log("error ", error);
})
}
$scope.fetch = function () {
BaasBox.loadCollection("Playlists")
.done(function (res) {
$scope.playlists = res;
})
.fail(function (error) {
console.log("error ", error);
})
}
})
View:
<ion-view view-title="Playlists">
<ion-content>
<button ng-click="fetch()" class="button button-full button-positive">Fetch</button>
<ion-list>
<ion-item ng-repeat="playlist in playlists">
{{playlist.name}}
{{GetImage($index,playlist.image)}} <!-- call to resolve url - should definitely not be here-->
<img ng-src="{{playlist.img}}"></image>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
Update
This is still givig me a hard time. I'm now trying to solve it by creating a filter that resolves the name to a url like this <img ng-srv="{{playlist.image | getImage}}"></img>
but I can't figure out how to return anything from the async call in the filter..
Made another solution that also works, but it still looks horrible :S Help me Obi Wan Kenobi!
View:
<ion-view view-title="Playlists">
<ion-content>
<button ng-click="fetch()" class="button button-full button-positive">Fetch</button>
<ion-list>
<ion-item ng-repeat="playlist in playlists">
{{playlist.name}}
<img ng-src="{{playlist.img}}"></image>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
Ctrl:
.controller('PlaylistsCtrl', function ($scope) {
$scope.playlists = [];
$scope.fetch = function () {
BaasBox.loadCollection("Playlists")
.done(function (res) {
$scope.playlists = res;
resolveImageUrls($scope.playlists);
})
.fail(function (error) {
console.log("error ", error);
})
}
function resolveImageUrls(list) {
angular.forEach(list, function (item, key) {
BaasBox.getImageURI(item.image, { 'resizeId': 1 })
.done(function (url) {
item.img = url['data'];
$scope.$apply();
})
.fail(function (error) {
console.log("error ", error);
})
});
}
})