2

How to show image in angularjs view from Firebase Storage reference? I can print url in console but can't access in view.

Service Code

function getImageUrl(image) {
        var url;
        var pathReference = firebaseDataService.images.child(image);
        return pathReference.getDownloadURL().then(function(url) {
           return url;                
        }).catch(function(error) {
            // Handle any errors
        });         
    }

Controller Code

$scope.getImageUrl = function(image) {
var imageUrl;
    var imageUrl = Service.getImageUrl(image).then(function(url) {  
          imageUrl = url;
          console.log(url);
          return imageUrl;
    });
   return imageUrl;
}

View

 <div ng-repeat="category in categories">
  <img src="{{getImageUrl(category.image)}}">
 </div>

2 Answers2

1

Using Angular markup like {{hash}} in a src attribute doesn't work right: The browser will fetch from the URL with the literal text {{hash}} until Angular replaces the expression inside {{hash}}. The ngSrc directive solves this problem.

<img ng-src="{{getImageUrl(category.image)}}">
Vicheanak
  • 6,444
  • 17
  • 64
  • 98
0

By the time you retrieve your categories you can loop over the categories and retrieve each image, store it in a diferent scope object and use it in your view.

$scope.images = {};

categoriesSnapshot.forEach(function(category){
    Service.getImageUrl(category.val().image).then(function(url) {  
          $scope.images[category.key] = url;
    }); 
});

In your HTML:

<div ng-repeat="(key, val) in categories">
    <img src="{{images[key]}}">
</div>
adolfosrs
  • 9,286
  • 5
  • 39
  • 67
  • Thanks for your solution,I found images were not visible due to It was due to digest cycle and I resolved by using $scope.$apply(). – Atul Sirpal Jul 13 '16 at 05:33