1

I´m an angular newbie

I have a directive with a template that looks something like this:

<div class="multiselect-styled">
    <a href="javascript:void(0);" class="link" id="link-{{ model.ControlId }}">
        <span class="options-selected" id="selected-options-{{ model.ControlId }}">TEXT</span>
        <span class="select-arrows"></span>
    </a>
</div>

My model contains an array called Options which contains n number of selectable options. Each option object has a boolean property; Selected. I would like to replace TEXT with a string saying n options selected. So, how do I get the number of selected options?

Of course, using a loop one would do something like this but is there some way I kind bind directly to n in this case (Find n with an expression)?

            var n = 0;
            angular.forEach(model.Options, function(option) {
                if (option.Selected)
                    n++;
            });

UPDATE 1

Options look like this:

"Options": [
      {
        "Id": "ApprovedAndClosed",
        "Value": "ApprovedAndClosed",
        "Text": "ApprovedAndClosed",
        "Selected": false,
        "Code": "ApprovedAndClosed"
      },
      {
        "Id": "Cancel",
        "Value": "Cancel",
        "Text": "Cancel",
        "Selected": false,
        "Code": "Cancel"
      },
      {
        "Id": "Done",
        "Value": "Done",
        "Text": "Done",
        "Selected": false,
        "Code": "Done"
      },
      {
        "Id": "Init",
        "Value": "Init",
        "Text": "Init",
        "Selected": false,
        "Code": "Init"
      },
      {
        "Id": "ReadyForProcessing",
        "Value": "ReadyForProcessing",
        "Text": "ReadyForProcessing",
        "Selected": false,
        "Code": "ReadyForProcessing"
      },
      {
        "Id": "Rejected",
        "Value": "Rejected",
        "Text": "Rejected",
        "Selected": false,
        "Code": "Rejected"
      },
      {
        "Id": "Reminder",
        "Value": "Reminder",
        "Text": "Reminder",
        "Selected": false,
        "Code": "Reminder"
      }
]
3
  • Can you provide the Options? Commented Jun 30, 2015 at 9:46
  • Yes, updated with Options. Commented Jun 30, 2015 at 9:56
  • Is it needed to be display in the view or just in your controller? Commented Jun 30, 2015 at 10:00

2 Answers 2

1

You can try using a filter:

{{(model.Options | filter:{'Selected': true}).length}}
Sign up to request clarification or add additional context in comments.

Comments

0

You could use lodash for this:

var countSelected = _.filter(Options, { 'Selected': true});
console.log(countSelected.length);

See: codepen example

Comments

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.