在控制器中创建一个函数,该函数将被调用并更新包含这些值总数的范围变量。
<tr ng-repeat="category in staff_categories">
<td>{{ category.name }}</td>
<td><input type="text" ng-model="category.permanent" ng-change="updatePermanentTotal()"></td>
<td><input type="text" ng-model="category.temp" ng-change="updateTemp()"></td>
</tr>
控制器代码
$scope.permanentTotal = 0.0;
$scope.tempTotal = 0.0;
$scope.updatePermanentTotal = function(){
$scope.permanentTotal = 0.0;
angular.forEach($scope.staff_categories, function(value, key){
if(!isNaN(parseInt(value.temp)){
$scope.tempTotal = $scope.tempTotal + parseInt(value.temp);
}
})
}
$scope.updateTemp = function(){
$scope.tempTotal = 0.0;
angular.forEach($scope.staff_categories, function(value, key){
$scope.tempTotal = $scope.tempTotal + parseInt(value.temp);
})
}
并将这两个总变量绑定到这样的两个输入。
Total permanent:<input type="text" ng-model="permanentTotal">
Total Temp:<input type="text" ng-model="tempTotal">