在加载数据之前加载角度控制器

时间:2014-11-15 22:17:18

标签: javascript angularjs

我正在编写一个从API加载电影信息的简单应用。在加载之后,我试图使用Angular在简单的列表视图中显示电影。我正确加载电影,但似乎创建了角度控制器,并在填充电影数组之前将影片数组发送到视图。我不确定如何解决这个问题。

var movieList = [];
var app = angular.module('top250', []);

// immediately make a call to the server to get data (array of movies from text file)
$.post('/', {}, function(data) {
    init(data);
});

app.controller('MovieController', function() {
    // should be setting this.movies to an array of 250 movies
    this.movies = movieList;
});

function init(data) {
    // cycle through array, use title to retrieve movie object, add to array to be sent to view

    $.each(data, function(index, value) {
        var title = value.split(' (')[0];
        title = title.split(' ').join('+');
        var url = 'http://www.omdbapi.com/?t=' + title + '&y=&plot=short&r=json';

        $.getJSON(url, function(data) {
            console.log('in get json', data);
            var movieObj = data;
            storeMovie(movieObj);
        });

    });
}

function storeMovie(movieObj) {
    movieList.push(movieObj);
}

我的HTML(虽然我确定这不是问题所在:

<body ng-controller="MovieController as MovieDB">

  <div class="row">
    <div class="large-12 columns">
      <h1>IMDB Top 250 List</h1>
    </div>
  </div>

  <div class="row">
    <div class="large-12 columns" id="movie-list">

    <div class="list-group-item" ng-repeat="movie in MovieDB.movies">
      <h3>{{movie.Title}} <em class="pull-right">{{movie.Plot}}</em></h3>


    </div>
  </div>

  <script src="js/foundation.min.js"></script>
  <script>
    $(document).foundation();
  </script>
</body>

3 个答案:

答案 0 :(得分:1)

首先,我将你的ajax调用转换为角度工厂:

app.factory('MoviesService', function($http, $q) {

  function getMovie(value) {
    var title = value.split(' (')[0];
    title = title.split(' ').join('+');
    var url = 'http://www.omdbapi.com/?t=' + title + '&y=&plot=short&r=json';
    return $http.get(url).then(function(res){ return res.data; });
  }

  return $http.post('/').then(function(res) {
    return $q.all(res.data.map(getMovie));
  });
});

然后我可以像这样消费它:

app.controller('MovieController', function(MoviesService) {

    var self = this;

    MoviesService.then(function(movies) {
      self.movies = movies;
    });
});

答案 1 :(得分:0)

  1. 不要使用jquery
  2. 使用angular $ http或$ resource
  3. 使用$ http,你将范围var设置为promise中的数据,生活将会很好

答案 2 :(得分:-1)

您需要等待init方法完成:

function init(data, complete) {

    $.each(data, function(index, value) {
        var title = value.split(' (')[0];
        title = title.split(' ').join('+');
        var url = 'http://www.omdbapi.com/?t=' + title + '&y=&plot=short&r=json';

        $.getJSON(url, function(data) {
            console.log('in get json', data);
            var movieObj = data;
            storeMovie(movieObj);

        }).always(function(){ // count competed ajax calls,
                              // regardless if they succeed or fail
            if(index === data.length -1)
                complete(); // call callback when all calls are done
        });

    });


}

现在你可以这样做:

app.controller('MovieController', function() {
    $.post('/', {}, function(data) { 
        init(data, function(){
            this.movies = movieList;
        });
    });
});

就个人而言,我会将movieList保留在init方法的内部,并在您完成后通过回调发送,但这只是一个偏好。