调用过滤器的次数

时间:2014-11-01 13:21:02

标签: angularjs angularjs-filter

假设像

这样的过滤器
app.filter('unread', function () {
  return function (note) {
    console.log(note);
    return (note.status == 'unread');
  };
});

我在$ rootScope

中的数组上使用此过滤器
<span ng-class="(note| unread).length == 0 ? '' : 'active'">{{value.length}}</span>

其中$rootScope.note是一个数组。 span元素在ng-view之外,它与$ scope无关,我在$ scope中有很多对象数组。

我认为过滤器会将注释记录在$ rootScope.note.length的数量中。但它记录了更多的内容,我无法找出$ rootScope,$ scope和控制台中的日志元素之间的合理关系。你可以解释一下吗?

编辑:

过滤器已更正。

1 个答案:

答案 0 :(得分:1)

您对过滤器的html调用不正确吗? 尝试删除:note,如下所示:

<span ng-class="(note| unread).length == 0 ? '' : 'active'">{{value.length}}</span>

分号后面的内容是附加参数。例如,如果您只想过滤重要的未读笔记:

app.filter('unread', function () {
  return function (note, type) {
    console.log(note);
    return (note.status == 'unread' && note.type == type);
  };
});

<span ng-class="(note| unread:'important').length == 0 ? '' : 'active'">{{value.length}}</span>