我正在开发一个使用angularjs的迷你app,它可以从新闻api中获取数据。我已成功使用for循环(下面)从10个新闻源(由api提供)的数组中获取10个数组(只是我想要的数量)。问题是视图中的ng-repeat只显示循环的最后一次迭代。如何让它显示所有迭代?
这是控制器:
alias cc=`which gcc`
循环为我提供了所有必需的文章,但我不知道如何编写ng-repeat来显示所有数据而不是仅显示最后一次迭代
和html:
angular.module('newsApp',[])
.controller('newsController',['$scope','$http', function($scope,$http){
var index = 0;
var sortby = ['top','latest','popular'];
$http.get('https://newsapi.org/v1/sources?language=en').then(function(response){
var sourceIdArray = [];
var articlesArray = [];
for (var i = 0; i < 9; i++) {
$scope.getId = response.data.sources[i].id;
sourceIdArray.push($scope.getId);
$http.get(' https://newsapi.org/v1/articles?source=' + sourceIdArray[i] + '&apiKey=53bec2b512724f58b92203f0f7e93dc1').then(function(response){
$scope.comits = response.data.articles
articlesArray.push($scope.comits);
});
}
})
}])
任何帮助将不胜感激!
答案 0 :(得分:0)
您正在使用articlesArray.push($ scope.comits)将文章推送到articlesArray;
您需要使articlesArray成为$ scope的成员,然后使用ng-repeat访问它。 $ scope.comits仅包含根据您的代码检索的最后一篇文章。
或者将comits声明为$ scope.comits = []然后将$ scope.comits = response.data.articles更改为$ scope.comits.push(response.data.articles)