我正在尝试这样做:
- ng-重复大约 50 项的列表
- 根据文本字段对其进行过滤 (
ng-repeat="note in Notes|filter:basicFilter"
) - 如果基本过滤器未产生任何结果(或者可能有 5 个结果),请执行 $http 调用以对数据库进行深度搜索
有没有办法检查过滤器在“基本”过滤器之后产生的结果,以便我可以执行 $http 回调到服务器?是否有一个内置于 Angular “过滤器”可供我调用?
我尝试观察变量:
$scope.$watch('filtered_notes', function() {
// can't do this
});
其中 ng-repeat="filtered_notes 中的注释 = (notes | orderBy:['-note_time']|filter:noteFilter|limitTo:limit)"
但是,它会抛出一个错误:未捕获错误:[$rootScope:infdig] 已达到 10 $digest() 迭代。正在中止!
最佳答案
您可以创建自定义过滤器,如下例所示
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;
};
关于javascript - 检查 ng-repeat 在过滤器后是否产生空集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25430698/