Angularjs在ng-click函数中传递参数值

时间:2014-05-22 22:05:54

标签: javascript angularjs

我有一个角度应用,我有一个定义为sumCompletedCredits的自定义过滤器。这是电话的html:

{% 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%}

以下是自定义过滤器定义:

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;
         }
        };
});

这是我想要实现的目标:我想在按钮的ng-click()函数中传递第3个参数,其中参数将是上述范围中的值,如下所示:

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

所以基本上我试图发送将在按钮之前显示在span徽章中的值,并将该值作为第三个参数发送。我如何发送第三个细节?

1 个答案:

答案 0 :(得分:5)

您可以将过滤结果分配给范围属性,并在函数中引用该属性:

<span ...
        ng-bind="summed=(mpttdetails | filter:{category.id:{{node.id}}} | 
                                       sumCompletedCredits)">
</span>
...
<button ...
        ng-click="open({{node.id}}, {{node.min_credit}}, summed)">
    ...
</button>