title: Filtering a page with infinite scroll in AngularJS
body: <p>I implemented an infinite scrolling feature on my AngularJS + Node.js app.</p>
  
  <p>It is based on this JSfiddle and works the same way: <a href="http://jsfiddle.net/vojtajina/U7Bz9/">http://jsfiddle.net/vojtajina/U7Bz9/</a>
  HTML:</p>
  
  <pre><code>&lt;div id="fixed" when-scrolled="loadMore()"&gt;
    &lt;ul&gt;
     &lt;li ng-repeat="i in items"&gt;{{i.id}}&lt;/li&gt;
    &lt;/ul&gt;  
  &lt;/div&gt;​
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>function Main($scope) {
    $scope.items = [];
  
    var counter = 0;
     $scope.loadMore = function() {
       for (var i = 0; i &lt; 5; i++) {
          $scope.items.push({id: counter});
          counter += 10;
       }
     };
  
    $scope.loadMore();
  }
  
  angular.module('scroll', []).directive('whenScrolled', function() {
    return function(scope, elm, attr) {
      var raw = elm[0];
  
      elm.bind('scroll', function() {
          if (raw.scrollTop + raw.offsetHeight &gt;= raw.scrollHeight) {
              scope.$apply(attr.whenScrolled);
          }
      });
    };
  });
  </code></pre>
  
  <p>​</p>
  
  <p>My reasons for implementing the infinite scroll is in order to save my users bandwidth by not loading all 1000 results and their corresponding images unless the user wants to see all of it.</p>
  
  <p>However when searching within the results using an AngularJS filter I am encountering problems, because of course not all the results are there (unless the user has scrolled to the very bottom) so the search will only return a fraction of the required results.</p>
  
  <p>I then removed the infinite scrolling feature in order to have the search function work properly but this provided new issues on chrome (not on Firefox though) when I open the page the browser starts loading images from the top. If I then filter the results by searching for something starting with "z" (at the very bottom of the results) Firefox switches focus and loads the results starting with "z" first (as they are then the only ones being displayed). However chrome continues loading through the list and thus the user will only see the results of their search (for "z") once all the images in the app have been loaded.</p>
  
  <p>I am looking for a way to have angular provide both the infinite scroll and a proper search filter on the results at the same time. If this is not possible then a way to make chrome load the visible images first.</p>
  
  <p>I am currently trying some weird stuff with a bunch of different arrays in the scope but I haven't had any luck so far.</p>