angularjs中所有sum列的和
我有长度和呼吸,基于我计算面积(l*b),假设我有许多不同长度和呼吸的字段,面积计算正确,但我需要面积列的总和,那么如何在一个新的文本框中添加上述所有字段的总和???我需要所有num3(面积)的总和 我的代码在下面angularjs中所有sum列的和,angularjs,Angularjs,我有长度和呼吸,基于我计算面积(l*b),假设我有许多不同长度和呼吸的字段,面积计算正确,但我需要面积列的总和,那么如何在一个新的文本框中添加上述所有字段的总和???我需要所有num3(面积)的总和 我的代码在下面 长度: 宽度: 面积: - 添加字段 var-app=angular.module('myapp',[]); 应用程序控制器('MainCtrl',函数($scope){ $scope.choices=[{id:'choice1'},{id:'choice2'}]; $scope
长度:
宽度:
面积:
-
添加字段
var-app=angular.module('myapp',[]);
应用程序控制器('MainCtrl',函数($scope){
$scope.choices=[{id:'choice1'},{id:'choice2'}];
$scope.addNewChoice=函数(){
var newItemNo=$scope.choices.length+1;
$scope.choices.push({'id':'choice'+newItemNo});
};
$scope.removeChoice=函数(){
var lastItem=$scope.choices.length-1;
$scope.choices.splice(最后一项);
};
});
对于你的情况,我不知道一个简单的解决办法
您不能使用value=“{val+val2}}”,因为它仅用于显示值而不是更改模型
一种可能的解决办法:
<form data-ng-repeat="choice in choices">
Length:<input type="number" ng-model="choice.length" />
width: <input type="number" ng-model="choice.width" />
Area: <input id="area" type="number" placeholder="Area" value="{{ choice.length * choice.width }}" />
<button ng-show="$last" ng-click="removeChoice()">-</button>
</form>
<button ng-click="addNewChoice()">Add fields</button>
Result : {{ sum() }}
长度:
宽度:
面积:
-
添加字段
$scope.total=函数(){
var合计=0;
angular.forEach($scope.choices,函数(项){
合计+=item.num1*item.num2;
})
返回总数;
}
请毫不犹豫地验证我的答案;)当创建一篇文章时,请确保它的格式与您的预期一致。
$scope.choices = [{id: 'choice1', length:0, width: 0}, {id: 'choice2', length:0, width: 0}];
$scope.addNewChoice = function() {
var newItemNo = $scope.choices.length+1;
$scope.choices.push({'id':'choice'+newItemNo});
};
$scope.removeChoice = function() {
var lastItem = $scope.choices.length-1;
$scope.choices.splice(lastItem);
};
$scope.sum = function() {
var sum = 0;
angular.forEach($scope.choices, function(choice) {
sum += choice.length * choice.width;
});
return sum;
}
<form data-ng-repeat="item in choices">
Length:
<input type="number" ng-model="item.num1" /> width:
<input type="number" ng-model="item.num2" /> Area:
<input id="area" type="number" ng-model="num3" placeholder="Area" value="{{item.num1 * item.num2}}" />
<button ng-show="$last" ng-click="removeChoice()">-</button>
</form>
<span data-ng-bind="'Total Area:' + total()"></span>
</br>
<button ng-click="addNewChoice()">Add fields</button>
$scope.total = function() {
var total = 0;
angular.forEach($scope.choices, function(item) {
total += item.num1 * item.num2;
})
return total;
}