深入我的http请求不会在视图上显示所需的数据

GY22

因此,建立在上一个问题上(服务中的Http请求成功但无法在视图中显示)。我需要更深入地了解我的http请求以对选定的电影进行api调用,如下所示:

http.get('https://api.themoviedb.org/3/movie/'+ movieId + '?api_key=XXX')

我的服务的完整代码:

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;
})

我的控制器如下:

.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
    });
})

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>

单击其中一部电影时,它应该转到详细信息页面(ID显示在URL中),但我似乎无法读取该特定电影的数据。虽然我所有的请求都可以,并且可以通过控制台进入。

详细信息页面的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>

详细信息页面的屏幕截图: 在此处输入图片说明

我究竟做错了什么 ?

贾斯珀·塞尔夫

显示消息“诺言已实现”,因此您知道第一个请求已完成,但这并没有告诉您有关moviedetail-call的任何信息。

您输入一个循环以检索所有“热电影”详细信息。但是,由于d.resolve第一个调用的成功代码中包含,因此在完成moviedetails调用之前会解决诺言。

我会(从所有detail调用中)组成一个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;
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

值不会在视图上更新

我的视图不会在 mvc 中显示我的表数据

数据不会在带有Codeigniter的视图中显示

angularjs $广播不会在视图上反映值

Bootstrap 导航栏不会在移动视图上折叠

addSublayer不会在视图上添加任何子层

Google的GeoChart不会在爱尔兰地图上显示软木

core-list不会在滚动视图中显示我的所有项目吗?

Android 不会在 CardView 中将视图显示为线

为什么我的 InnerHTML 代码不会在表单字段中显示任何数据?

Vuex 调度不会在视图中返回数据

数据不会在Angular视图中加载

视图不会在 MVC 中返回模型数据

ASP.NET Core MVC - 数据不会在发布请求中从视图传递到控制器

我的文字不会在 pygame 中显示

我的YouTube视频不会在iframe中显示

Angular 不会在 HTML 中显示我的数组结果

应用程序不会在recyclerview火力显示数据

更改后角度8值将不会在视图上更新

使用jQuery中的常见div ID,按钮不会在多个视图上消失

自定义注释在更新后不会在地图视图上更新

为什么AndroidStudio告诉我该目录已经存在,但不会在“项目”视图中显示?

不会在地图上显示城市和类似地点

为什么 stat_summary 不会在我的条形图上添加标签?

AJAX 不会在第一个请求中显示图像,但会在连续请求中显示

Angularjs-Charts.js:同一图表元素不会在其他视图上重绘

Div不会在悬停时显示

片段不会在活动中显示

活动不会在点击时显示