检查ng-repeat是否在过滤后产生空集

时间:2014-08-21 15:52:17

标签: javascript angularjs angularjs-ng-repeat

我正在尝试这样做:

  • ng-重复约50项的列表
  • 根据文字字段(ng-repeat="note in notes|filter:basicFilter"
  • 对其进行过滤
  • 如果基本过滤器没有结果(或者可能是5个结果),请执行$ http调用以对数据库进行深度搜索

有没有办法检查过滤器在“基本”过滤器之后产生什么,所以我可以执行$ http回调服务器?是否有一个内置于角度的“过滤器”,我可以调用它?

我试过看变量:

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

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

但是,它会引发错误:Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

1 个答案:

答案 0 :(得分:1)

您可以创建自定义过滤器,例如以下示例

<强> HTML

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

自定义过滤器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;
  };