【发布时间】:2016-02-17 07:09:10
【问题描述】:
我正在尝试使用 AngularJS 创建一个跟踪器,但在涉及 ng-repeat 时我的逻辑有点问题。
这是我的表格的样子,以帮助解释我正在尝试做的事情:
所以照片中的文字应该是MTD1 = 5、MTD2 = 15、MTD3 = 18等。与total列相同(total列加起来actual列)。
我需要确保两件事在执行此操作时仍然有效:
1) 我想确保它在用户输入新值时实时更新数字。
2) 我还想以一种允许我在我的数据库中更新它的方式构建它(在为每一行计算总数之后保留总数)。
这是我目前的代码:
<tr ng-repeat="i in new() track by $index">
<td>{{$index + 1}}</td>
<td>
<input type='text' ng-model="i.ctn_goal">
</td>
<td>
<input type='text' ng-model="i.ctn_actual">
</td>
<td>{{ }}</td>
<td>{{ }}</td>
<td>
<input type='text' ng-model="i.twi_actual">
</td>
<td>
<input type='text' ng-model="i.acc_goal">
</td>
<td>
<input type='text' ng-model="i.acc_actual">
</td>
<td>{{ }}</td>
<td>{{ }}</td>
<td>
<input type='text' ng-model="i.acc_units">
</td>
<td>
<input type='text' ng-model="i.mpp_goal">
</td>
<td>
<input type='text' ng-model="i.mpp_actual">
</td>
<td>{{ }}</td>
<td>{{ }}</td>
</tr>
还有我的控制器:
app.controller("tableCtrl", function ($scope, $http) {
// Generate rows for each day of the month
$scope.new = function () {
var x = new Date();
var date = new Date(x.getYear(), x.getMonth()+1, 0).getDate();
return new Array(date);
}
});
假设我可以在控制器中访问它们,我将 ng-model 附加到输入,但我认为我对 ng-repeat 及其工作原理缺乏一些了解。
编辑:{{ items[$index].ctn_goal + items[$index-1].ctn_goal }} 用于 CTN -> MTD 列。
【问题讨论】:
-
在您的 $scope.new = function () 中,与 ng-repeat 的对象 i 相关的所有参数在哪里。表示您在哪里传递值 i.ctn_goal 等等
-
它们不存在。我不知道如何将它们传递给我的控制器或从我的控制器访问它们。我最初添加它们是因为我想自己在
{{ }}字段中做一些数学运算。前任。{{ i[0].acc_actual + i[1].acc_actual }}.. 愚蠢,我知道。这就是我在这里的原因。
标签: javascript angularjs angularjs-scope angularjs-ng-repeat angular-ngmodel