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>
5
  • you should be retrieving the images url by the time you retrieve the categories and store them in a diferent obj. <img src="{{images.categoryId}}"> Commented Jul 11, 2016 at 17:43
  • I am getting categories in seperate service method and than in ng-repeat i am calling storage service to get image url. Commented Jul 11, 2016 at 17:53
  • Let me see the method that retrieves the categories. Commented Jul 11, 2016 at 17:55
  • What comes in console.log(url);? Commented Jul 11, 2016 at 18:08
  • @AtulSirpal Let me know if you made any progress or if you still have any doubts. Commented Jul 12, 2016 at 16:32

2 Answers 2

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)}}">
Sign up to request clarification or add additional context in comments.

1 Comment

Still not working,I am not sure but i think this is due to promise
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>

1 Comment

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().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.