如何从Firebase的存储中检索图像的URL?

时间:2017-02-14 14:02:53

标签: javascript angularjs firebase ionic-framework firebase-storage

我直接从Firebase文档中获取此代码:

// Create a reference to the file we want to download
  var user = firebase.auth().currentUser.uid;
  var storageRef = firebase.storage();
  var pathReference = storageRef.ref('/' + user + '/profilePicture/' + image.name);

  //var starsRef = storageRef.child('/' + user + '/profilePicture/' + file.name);

  // Get the download URL
  pathReference.getDownloadURL().then(function(url) {
  // Insert url into an <img> tag to "download"
      $scope.imageUrl = url;
      console.log($scope.imageUrl);
  });

但我想知道在我的html文件中要写什么才能显示图像......有什么帮助?

1 个答案:

答案 0 :(得分:1)

只需将结果放在控制器的变量中,然后放在HTML上的ng-src中。

最好的方法是使用服务来获取URL而不是直接在控制器中。

<强> 控制器

app.controller('MyController', ['$scope', function($scope) {
    // Create a reference to the file we want to download
    var user = firebase.auth().currentUser.uid;
    var starsRef = storageRef.child('/' + user + '/profilePicture/' + file.name);

    // Get the download URL
    starsRef.getDownloadURL().then(function(url) {
    // Insert url into an <img> tag to "download"
        $scope.imageUrl = url;
    });
}]);

<强> HTML

<img ng-src="{{imageUrl}}"/>