我试图总结一列数字,但是当我使用下面的代码时,"总计"字段是空的。关于我可能做错什么的任何想法?
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;
};
})
答案 0 :(得分:2)
您似乎正在使用controllerAs
语法,例如ng-controller="ProjectListCtrl as projectList"
有了这个,您需要从视图中调用projectList.total()
,而不仅仅是total()