Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@abject_error 's answer using <code>$timeout</code> does work. I edited your fiddle with his suggestion and made this <a href="http://jsfiddle.net/nackjicholson/5p6vF/2/">jsFiddle</a></p> <p><strong>CAVEAT</strong></p> <p>I think the solution is indicative of a bigger problem in the form of a race condition!</p> <h1><a href="http://jsfiddle.net/nackjicholson/5p6vF/">jsFiddle using filterFilter and $watch</a></h1> <p>and that fiddle is the way around it for reals.</p> <p><strong>And here is the explanation</strong></p> <p>Your race condition is between handling the change of <code>search</code> and the availability of <code>$scope.filtered</code>.</p> <p>I think the culprits to eliminate in order to resolve this race condition are:</p> <pre><code>ng-model="search" ng-change="filter()" </code></pre> <p>and</p> <pre><code>ng-repeat="data in filtered = (list | filter:search)......." </code></pre> <p>Using <code>ng-change</code> to fire off "filter()" to do the calculation of <code>noOfPages</code> but also depending on a change in search in order to create <code>filtered</code>. Doing it this way, ensures the filtered list cannot possibly be ready in time to calculate the number of pages, and that's why hobbling "filter()" by 10ms with a timeout gives you the illusion of a working program.</p> <p>What you need is a way to "watch" <code>search</code> for changes, and then filter the list in a place where you have access to both creating <code>$scope.filtered</code> and calculating <code>$scope.noOfPages</code>. All in sequence, without a race.</p> <p>Angular has that way! It is possible to use the <code>filter</code> filter in your controller as function very poorly named: <code>filterFilter</code>. Check this out in the <a href="http://docs.angularjs.org/guide/filter">Filters Guide - Using filters in controllers and services</a></p> <p>Inject it into the controller.</p> <pre><code>function pageCtrl($scope, filterFilter) { // ... } </code></pre> <p>Use it in a <code>$watch</code> function, documented in the <a href="http://docs.angularjs.org/api/ng.%24rootScope.Scope">scopes docs</a></p> <pre><code>$scope.$watch('search', function(term) { // Create filtered $scope.filtered = filterFilter($scope.list, term); // Then calculate noOfPages $scope.noOfPages = Math.ceil($scope.filtered.length/$scope.entryLimit); }) </code></pre> <p>Change the template to reflect our new way. No more in DOM filter, or <code>ng-change</code></p> <pre><code>&lt;input type="text" ng-model="search" placeholder="Search"/&gt; </code></pre> <p>and</p> <pre><code>&lt;li ng-repeat="data in filtered | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit"&gt; {{data.name}} &lt;/li&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload