【发布时间】:2016-11-26 04:25:34
【问题描述】:
我正在编写此函数以在我的 Angular 应用程序中使用来评估 ng-repeat 列表中的 ng-src。我需要使调用同步,以便每次调用函数时值都是正确的。问题是:
为什么这段代码会返回一个值:
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 返回 url?
这是文档链接:https://firebase.google.com/docs/storage/web/download-files
最后我会把它变成一个 $scope 函数来使用类似于这个:
<div ng-repeat="message in messages">
<img ng-src="{{ getImageUrl(message.time) }}">
</div>
【问题讨论】:
标签: javascript angularjs firebase firebase-storage