我正在編寫此函數以在我的角度應用程序中用於評估ng-repeat列表中的ng-src。我需要使調用同步,以便每次調用該函數時該值都是正確的。問題是:從Firebase存儲器返回下載URL的函數
爲什麼這個代碼返回值:
var storage = firebase.storage();
var returnVal; //causes sync issues
var getImageUrl = function (time) {
storage.ref('images/' + time + '.jpg').getDownloadURL().then(function (url) {
returnVal = url;
});
return returnVal;
};
但是,這並不工作:
var storage = firebase.storage();
var getImageUrl = function (time) {
var returnVal; //so that this is specific to the function
storage.ref('images/' + time + '.jpg').getDownloadURL().then(function (url) {
returnVal = url; //I simply want to return the value of 'url'
});
return returnVal;
};
任何想法如何,我可以讓getImageUrl()函數返回來自.then的網址?
這是文檔鏈接:https://firebase.google.com/docs/storage/web/download-files
最後我會變成一個$範圍功能使用與此類似:
<div ng-repeat="message in messages">
<img ng-src="{{ getImageUrl(message.time) }}">
</div>
參見https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud -functions換火力 – Kato