服务中的Http请求成功但无法在视图中显示

时间:2015-10-15 14:24:51

标签: angularjs ionic-framework

我正在与Ionic和The Movie Database的api合作。我写了一个服务来使我的http请求回来都很好。我在console.log中获取了所有输出但由于某种原因我仍然无法在视图中显示数据。所以我想知道在双向数据绑定方面我是否做错了

我的服务代码:

angular.module('starter.services', [])

.service('HotMoviesService', function($http, $q){
    var final_url = "https://api.themoviedb.org/3/movie/popular?api_key=XXXX";

    var self = {
        'hotMovies' : [],
        'loadHotMovies' : function() {
            var d = $q.defer();
            $http.get(final_url)
            .success(function success (data){
                console.log(data);
                self.hotMovies = data.results;
                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.js代码:

angular.module('starter.controllers', ['ionic.contrib.ui.hscrollcards', 'starter.services'])

.controller('StartCtrl', function($scope, $http, HotMoviesService) {

    $scope.hotmovies = [];

    HotMoviesService.loadHotMovies().then(function success (data){
        console.log(data);
        $scope.hotmovies = HotMoviesService.hotmovies;
    },
    function error (data){
        console.log(data)
    });
})

我的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>

以下是我的控制台的屏幕截图,因为您可以看到一切正常:

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要hotMovies,请注意&#34; m&#34;情况下:

$scope.hotmovies = HotMoviesService.hotMovies;