Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Assuming the following data:

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

I am trying to find the count of objects in the array where attended is true. I have tried the following:

rosters.html:

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

rosters-controller.js:

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

The html filter works as expected, returning 2 in this instance. However, the function version encounters the error ReferenceError: Can't find variable: attended. I assume that there is something trivial that I've missed in the function, but I am not sure what it is.

share|improve this question
1  
Please compare attentively both of your versions. If you can't find what's going on, have a look at the expected type of the arguments. –  Blackhole Oct 17 '14 at 16:33
    
Thanks for pointing that out. I had skimmed that page, but I completely misunderstood how to build the expression argument. After seeing @tasseKATT's answer, it totally makes sense now. –  The DIMM Reaper Oct 17 '14 at 16:41
    
Could the downvoter please explain? My question turned out to have a simple answer, but I didn't think it was poor according to this Meta post. –  The DIMM Reaper Oct 23 '14 at 4:03

1 Answer 1

up vote 2 down vote accepted

Use an object as the expression:

return $filter('filter')($scope.roster, { attended: true }).length;
share|improve this answer
1  
Thanks so much, I'm kicking myself for not seeing that before. –  The DIMM Reaper Oct 17 '14 at 16:42
    
You're welcome :) –  tasseKATT Oct 17 '14 at 16:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.