如何从ng-repeat指令创建数字总和

时间:2015-02-19 21:03:11

标签: angularjs angularjs-ng-repeat

我试图总结一列数字,但是当我使用下面的代码时,"总计"字段是空的。关于我可能做错什么的任何想法?

HTML:

<tr ng-repeat="project in projectList.projects>   
    <td>{{project.description}}</td>
    <td>{{project.type.cost | currency}}</td>
</tr>

<h2>Total: {{ total() | currency }}</h2>

使用Javascript:

myApp.controller('ProjectListCtrl', function ProjectListCtrl(Projects) {
var projectList = this;
projectList.total = function(){
    var total = 0;
    angular.forEach(projectList.projects, function(project) {
        total += project.type.cost;
    });
    return total;
};
})

1 个答案:

答案 0 :(得分:2)

您似乎正在使用controllerAs语法,例如ng-controller="ProjectListCtrl as projectList"

有了这个,您需要从视图中调用projectList.total(),而不仅仅是total()