1

I have an angular app where i have a custom filter defined as sumCompletedCredits. Here is the html of the call:

{% if node.is_leaf_node %}
     <span class="badge badge-success" 
        ng-bind="mpttdetails | filter:{category.id:{{node.id}}} | sumCompletedCredits">
    </span>

    <div class="row">
        <button class="btn btn-default btn-sm pull-right" 
            ng-click="open({{node.id}}, {{node.min_credit}})">Add Course <span class="glyphicon glyphicon-plus"></span>
        </button>
    </div>
{%endif%}

Here is the custom filter definition:

app.filter('sumCompletedCredits', function () {
    return function (mpttdetails) {
        if(typeof mpttdetails === 'undefined'){
                return;
        }
        else {
            var sum = 0;
            mpttdetails.forEach(function (detail) {
                sum += detail.student_academic_credit.credit;
            });
            return sum;
         }
        };
});

Here is what i want to achieve: I would like to pass a 3rd parameter in the button's ng-click() function where the parameter would be the value from the above span like the follows:

ng-click="open({{node.id}}, {{node.min_credit}}, *{{{this content will be the value in the badge class from the custom filter}}}*)"

So basically i am trying to send the value that will be displayed in the span badge just before the button and send that value as the third parameter.How do i send the third detail?

3
  • Have you tried just putting the same code / filter in the function? Commented May 22, 2014 at 23:11
  • Yeah I tried it but the ng-click definition was being violated and it threw me an error Commented May 23, 2014 at 0:44
  • Possible for you to import filters in your controller / directive and then call it inside your open function? $filter('filtername')(arg); Commented May 23, 2014 at 7:39

1 Answer 1

5

You can assign the result of the filtering to a scope property and reference that property in your function:

<span ...
        ng-bind="summed=(mpttdetails | filter:{category.id:{{node.id}}} | 
                                       sumCompletedCredits)">
</span>
...
<button ...
        ng-click="open({{node.id}}, {{node.min_credit}}, summed)">
    ...
</button>
Sign up to request clarification or add additional context in comments.

3 Comments

can we assign them like an array like this since my buttons and spans are in a loop and the values will be updated after every run? ng-bind="summed[{{node.id}}] = (mpttdeta...)"
Sure, but in that case you need to initialize summed in your controller. E.g. have a line like this: $scope.summed = [];

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.