1

I'm trying to do this:

  • ng-repeat a list of about 50 items
  • Filter it based on a text field (ng-repeat="note in notes|filter:basicFilter")
  • If the basic filter yields no results (or maybe like 5 results), perform an $http call to do a deep search on the database

Is there a way to check what the filter yields after the "basic" filter, so I can perform a $http call back to the server? Is there a "filter" that is built-in to angular that I can call up?

I've tried watching the variable:

  $scope.$watch('filtered_notes', function() {
    // can't do this
  });

Where ng-repeat="note in filtered_notes = (notes | orderBy:['-note_time']|filter:noteFilter|limitTo:limit)"

But, it throws an error: Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

1 Answers1

1

You can create custom filter like following example

HTML

<div ng-repeat="note in filtered_notes |  myCustomFilter:theInput">

custom filter JS

 iApp.filter('myCustomFilter', function() {
   return function( notes, input) {
    var filtered = [];

    angular.forEach(notes, function(item) {
       if(/* do any condition with 'input' */) {
          filtered.push(item);
        }

    });

    if(filtered.length >= 5 ){
         /* do HTTP call or use $broadcast to trigger HTTP call*/
    }

    return filtered;
  };
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225