0

我有這樣的代碼直接從火力地堡的文檔:如何從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

只要把你的結果放在控制器的變量裏面,並在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}}"/> 
+0

你好,感謝你的幫助,它顯示的CSS在我的圖片,但圖片本身是不是** **顯示(還)。我在imageUrl上嘗試了一個console.log,並且未顯示。可能是什麼原因 ? – FrenchyNYC

+0

你應該嘗試調試的是放置一個'console.log(url)'而不是imageUrl。如果你想調試imageUrl,你應該做'console.log($ scope.imageUrl)' – e666

+0

我不好意思,我忘記了存儲。現在我遇到的最後一個問題是「文件」沒有定義。任何想法我怎麼能有這些數據? 否則一切正常;) – FrenchyNYC

相關問題