2014-10-17 95 views
1

假设下面的数据阵列中的对象的属性:

var roster = [ 
    { 
     id: 1, 
     attended: true, 
     person: {printName: 'Larry'}}, 
    { 
     id: 2, 
     attended: false, 
     person: {printName: 'Curly'}}, 
    { 
     id: 3, 
     attended: true, 
     person: {printName: 'Moe'}}]; 

我试图找到对象的个数在那里出席了数组中是真实的。我曾尝试以下:

rosters.html:

{{ (roster | filter:{attended:true}).length }} 

名册-controller.js:

checkedInCount: function() { 
    return $filter('filter')($scope.roster, attended.true).length; 
} 

HTML筛选器正常工作,在这种情况下返回2 。但是,功能版本遇到错误ReferenceError: Can't find variable: attended。我认为在函数中有一些微不足道的东西,但我不确定它是什么。

+1

请比较用心的版本两种。如果你无法找到正在发生的事情,请查看参数的[预期类型](https://docs.angularjs.org/api/ng/filter/filter)。 – Blackhole 2014-10-17 16:33:32

+0

谢谢你指出。我已经浏览了该页面,但是我完全误解了如何构建“表达式”参数。看到@ tasseKATT的回答后,现在完全有意义。 – 2014-10-17 16:41:19

+0

可以请downvoter解释一下吗?我的问题原来有一个简单的答案,但我不认为这是穷人根据[这篇文章](http://meta.stackoverflow.com/questions/252677/when-is-it-justifiable-to -downvote-A-问题)。 – 2014-10-23 04:03:14

回答

2

使用对象的表达:

return $filter('filter')($scope.roster, { attended: true }).length; 
+1

非常感谢,我踢了自己没有看到之前。 – 2014-10-17 16:42:35

+0

不客气:) – tasseKATT 2014-10-17 16:49:46