Live search in AngularJS: updating results

I need a live search: the results are queried from the web api and updated as the user inputs.

The problem is that the list flickers and the text "No results" is displayed for a split second, even though the list of results remains the same. I think I need to remove and add elements with special code to avoid this, calculating the differences between arrays, etc.

Is there an easier way to avoid this flickering, at least, and perhaps be able to animate the changes?

It now looks like this:

How live search looks like

The html part:

    <div class="list-group">
        <a ng-repeat="test in tests track by test.id | orderBy: '-id'" ng-href="#/test/{{test.id}}" class="list-group-item">
            <h4 class="list-group-item-heading">{{test.name}}</h4>
            {{test.description}}
        </a>
    </div>
    <div ng-show="!tests.length" class="panel panel-danger">
        <div class="panel-body">
            No tests found.
        </div>
        <div class="panel-footer">Try a different search or clear the text to view new tests.</div>
    </div>

      

And the controller:

testerControllers.controller('TestSearchListCtrl', ['$scope', 'TestSearch',
function($scope, TestSearch) {
    $scope.tests = TestSearch.query();
    $scope.$watch('search', function() {
        $scope.tests = TestSearch.query({'q':$scope.search});
    });
}]);

      

+3


source to share


3 answers


You must use ng-animate to get the classes you need for smooth animation. For every ng-repeat element that has been moved, added or removed - angular will add specific classes. Then you can create these classes with CSS or JS so they don't flicker.



+2


source


An alternative way to do what you need is to use the angular-ui bootstrap Typeahead component (check at the bottom of the post). It has a forward-wait property in milliseconds, as well as a boilerplate for customizing it.



0


source


<div ng-app>
    <div ng-controller="MyController">
        <input type="search" ng-model="search" placeholder="Search...">
        <button ng-click="fun()">search</button>
        <ul>
            <li ng-repeat="name in names">{{ name }}</li>
        </ul>
        <p>Tips: Try searching for <code>ann</code> or <code>lol</code>

        </p>
    </div>
</div>



function MyController($scope, $filter) {
    $scope.names = [
        'Lolita Dipietro',
        'Annice Guernsey',
        'Gerri Rall',
        'Ginette Pinales',
        'Lon Rondon',
        'Jennine Marcos',
        'Roxann Hooser',
        'Brendon Loth',
        'Ilda Bogdan',
        'Jani Fan',
        'Grace Soller',
        'Everette Costantino',
        'Andy Hume',
        'Omar Davie',
        'Jerrica Hillery',
        'Charline Cogar',
        'Melda Diorio',
        'Rita Abbott',
        'Setsuko Minger',
        'Aretha Paige'];
    $scope.fun = function () {
        console.log($scope.search);
        $scope.names = $filter('filter')($scope.names, $scope.search);
    };
}

      

-1


source







All Articles