【发布时间】:2015-05-20 04:58:24
【问题描述】:
在我的控制器中,我需要一个函数,它接受一个参数并从服务器返回一个 URL。像这样的:
$scope.getPoster = function(title) {
return $http.get("http://www.omdbapi.com/?t=" + title + "&y=&plot=short&r=json");
};
在我看来,我需要以这种方式传递标题并获得src for ng-src 的结果:
<div class="col-sm-6 col-md-3" ng-repeat="movie in movies">
<div class="thumbnail">
<img class="poster" ng-src="{{getPoster(movie.Title)}}" alt="{{movie.Title}}">
<div class="caption">
<h3>{{movie.Title}}</h3>
<p>{{movie.Owner}}</p>
</div>
</div>
</div>
我知道$http 上的 HTTP 通信永远不会立即返回我需要的数据,因为所有通信都是异步的,所以我需要使用 promise:
$scope.getPoster = function(title) {
$http({ url: "http://www.omdbapi.com/?t=" + title + "&y=&plot=short&r=json" }).
then(function (response) {
// How can I return response.data.Poster ?
});
};
所以我的问题是,如何将响应作为 getPoster 函数的结果返回?
有什么想法吗?
【问题讨论】:
-
只是
return responde.data.Poster -
我不明白你的问题;你似乎已经添加了你需要的代码,然后在它周围加上了一条询问如何做的评论,大概没有测试过......
标签: javascript ajax angularjs promise angular-promise