繁体   English   中英

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

[英]Angular Controller Loading Before Data Is Loaded

我正在编写一个简单的应用程序,可从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>

首先,我将您的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. 不要使用jQuery
  2. 使用角度$ http或$ resource
  3. 使用$ http,您将scope var设置为promise中的数据,这样生活会很好

您需要等待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方法内,并在完成后与回调一起发送它,但这只是一个首选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM