GY2*_*Y22 6 angularjs ionic-framework
所以建立在前一个问题上(Http请求在服务中成功但无法在视图中显示).我需要深入了解我的http请求,为选定的电影进行api调用,如下所示:
http.get('https://api.themoviedb.org/3/movie/'+ movieId + '?api_key=XXX')
Run Code Online (Sandbox Code Playgroud)
我的服务的完整代码:
angular.module('starter.services', [])
.service('HotMoviesService', function($http, $q){
var movieId;
var final_url = "https://api.themoviedb.org/3/movie/popular?api_key=XXX";
var self = {
'hotMovies' : [],
'singleHotMovie' : [],
'loadHotMovies' : function() {
var d = $q.defer();
$http.get(final_url)
.success(function success (data){
//console.log(data);
self.hotMovies = data.results;
for(var i = 0; i < self.hotMovies.length; i++){
//console.log(self.hotMovies[i].id);
movieId = self.hotMovies[i].id;
//console.log("Logging movie id: ", movieId);
$http.get('https://api.themoviedb.org/3/movie/'+ movieId + '?api_key=XXXXX')
.success(function succcess(response){
//console.log("Logging response in the for loop " , response);
self.singleHotMovie = response;
})
.error(function error (msg){
console.log(msg);
})
}
d.resolve('The promise has been fulfilled');
})
.error(function error (msg){
console.error("There was an error retrieving the data " , msg);
d.reject("The promise was not fulfilled");
});
return d.promise;
}
};
return self;
})
Run Code Online (Sandbox Code Playgroud)
我的控制器如下:
.controller('StartCtrl', function($scope, $http, HotMoviesService) {
//POPULAR
$scope.hotmovies = [];
HotMoviesService.loadHotMovies().then(function success (data){
console.log(data);//returns success message saying that promise is fulfilled
$scope.hotmovies = HotMoviesService.singleHotMovie;
},
function error (data){
console.log(data)//returns error messag saying that promise is not fulfilled
});
})
Run Code Online (Sandbox Code Playgroud)
HTML代码(电影列表)
<ion-view view-title="The Movie Bank">
<ion-content class="background">
<h1 class="padding titleStart">Welcome to The Movie Bank</h1>
<div class="logo"></div>
<!-- HOT -->
<a class="customHref" href="#/app/hot">
<h1 class="padding customH1">Hot</h1>
</a>
<hscroller>
<ion-scroll direction="x" scrollbar-x="false">
<hcard ng-repeat="hotmovie in hotmovies">
<a href="#/app/hot/{{hotmovie.id}}">
<img ng-src="http://image.tmdb.org/t/p/w92/{{hotmovie.poster_path}}" >
</a>
</hcard>
</ion-scroll>
</hscroller>
</ion-content>
</ion-view>
Run Code Online (Sandbox Code Playgroud)
当点击其中一部电影时,它应该转到详细页面(ID显示在网址中),但我似乎无法读出该特定电影的数据.虽然我的所有请求都可以,但在我的控制台中通过.
详情页面的HTML代码:
<ion-view view-title="Hot Detail">
<ion-content >
<h1>test</h1>
<h4>{{original_title}}</h4>
<img ng-src="http://image.tmdb.org/t/p/w92/{{hotMovie.poster_path}}" >
</ion-content>
</ion-view>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?
显示消息"承诺已经完成",因此您知道第一个请求没有问题,但这并没有告诉您有关moviedetail调用的任何信息.
您输入一个循环以检索所有"热门电影"详细信息.但是,由于在d.resolve第一次调用的成功代码中,承诺在moviedetails调用完成之前得到解决.
我将制作一系列promisses(来自所有细节调用)并用于$q.all(promises);解决所有这些调用何时完成.这样你就知道你的所有数据了.
还有一件事...
您将响应复制到self.singleHotMovie中self.singleHotMovie = response;.这样,只有最后一个要解析的电影细节调用才会出现在self.singleHotMovie中.您可能希望使用self.singleHotMovie.push(response)它将其添加到数组中.
未经测试:
'loadHotMovies' : function() {
var d = $q.defer();
$http.get(final_url)
.success(function success (data){
//console.log(data);
self.hotMovies = data.results;
// create an array to hold the prommisses
var promisses = [];
for(var i = 0; i < self.hotMovies.length; i++){
//console.log(self.hotMovies[i].id);
movieId = self.hotMovies[i].id;
//console.log("Logging movie id: ", movieId);
var promise = $http.get('https://api.themoviedb.org/3/movie/'+ movieId + '?api_key=XXXXX')
.success(function succcess(response){
//console.log("Logging response in the for loop " , response);
self.singleHotMovie = response;
})
.error(function error (msg){
console.log(msg);
});
// add all the detail calls to the promise array
promisses.push(promise);
}
//when all the details calls are resolved, resolve the parent promise
$q.all(promisses).finally(function(){
d.resolve('The promise has been fulfilled');
});
})
.error(function error (msg){
console.error("There was an error retrieving the data " , msg);
d.reject("The promise was not fulfilled");
});
return d.promise;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
239 次 |
| 最近记录: |