1

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.

3
  • 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. Commented Oct 17, 2014 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. Commented Oct 17, 2014 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. Commented Oct 23, 2014 at 4:03

1 Answer 1

2

Use an object as the expression:

return $filter('filter')($scope.roster, { attended: true }).length;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, I'm kicking myself for not seeing that before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.