使用Angular 4,我需要显示一些数字的平均值。我的基本组成如下:Angular 4:从模板调用方法
@Component({
selector: 'app-home',
templateUrl: './home.component.html'
})
export class HomeComponent {
students:string[];
constructor(private studentsService: StudentsService) {}
ngOnInit() {
this.studentsService.getStudents().subscribe(students => {
...
})
function doAvg (arr) {
var i,
len=arr.length,
average=0,
obj={};
for (i=0;i<len;i++) {
average += arr[i];
}
return (average/arr.length);
}
}
}
我的模板有这个标记(GPARecord是float数组):
<tr *ngFor="let student of students">
...
<td>{{ doAvg(student.GPARecord) }}</td>
</tr>
但我发现了这个错误在控制台:
ERROR TypeError: _co.doAvg is not a function
帮助!谢谢!
谢谢!这些建议使它工作。 – jdstein1