如果没有足够的数据,如何在ng-repeat中加载更多数据?

时间:2019-10-21 15:58:15

标签: javascript angularjs api pagination

我在ng-repeat和ng-if条件下有一个过滤器,几乎每页总是少于10个结果(10个是我从API得到的结果)。

我的想法是在js文件中应该是一个有条件的循环

首先我们声明变量,以便我们检查长度

           var numberOfArticlesOnPage = container.children.length
           console.log(numberOfArticlesOnPage)

           for (i = 0; numberOfArticlesOnPage < 10); i++ {
               $scope.pageNumber = $scope.pageNumber + 1
               var i=1
               i++
               $http(
                   {
                       method: 'GET',
                       url: 

//Here I put i instead of page number so it loads next page and every time we create results[i] with new number in the end

'https://api.nytimes.com/svc/search/v2/articlesearch.json?fq=news_desk:("Politics")&page=' + i + '&api-key=groQeNemKAhk7QjDWircgauo5jYVcwez', /* Most recent */
                   }).then(function successCallback(result) {
                       $scope.results[i] = result.data.response.docs;
                   }, function errorCallback(result) {
                       console.log("there's a error, man...");
                   });
                //Check the length again
                   var container = document.querySelector(".container");
                   var numberOfArticlesOnPage = container.children.length```

//And that is the template where it should be shown

```<div class="container">
           <div class="box" ng-repeat="result in results | filter : '!trump'" ng-if="result.multimedia[2].url != null">
               <h2>{{result.headline.main}}</h2>
               <p class="abstract">{{result.abstract}}</p>
               <img src="https://static01.nyt.com/{{result.multimedia[0].url}}">
               <p></p>
               <a ui-sref="article ({
               articleSlug:'{{result.headline.main | slugify}}', 
               title: '{{result.headline.main}}',
               url: '{{result.web_url}}', 
               image: 'https://static01.nyt.com/{{result.multimedia[0].url}}',
               externalUrl: '{{result.url}}'
               })">Read more...</a>
           </div>
       </div>```


//But I don't know how to write a code in the template so it can take more data?

0 个答案:

没有答案