angular.module("myApp",[])
.controller("inputController",function($scope){
$scope.items=[]; //初始化数组,以便为每一个ng-model分配一个对象
var i=0;
$scope.getResult=function(){ //计算输入框的总值
var result=0;
angular.forEach($scope.items,function(item,key){
result+=parseInt($scope.items[key]);
})
$scope.result=result;
}
$scope.Fn= {
add: function () { //每次添加都要给items数组的长度加一
$scope.items[i] = 0;
i++;
},
del: function (key) { //每次删除一个输入框都后要让i自减,否则重新添加时会出bug
console.log(key);
$scope.items.splice(key, 1);
i--;
$scope.getResult(); //每次删除时得重新计算总值
}
}
})
<body ng-controller="inputController">
<div ng-repeat="(key,item) in items track by $index"> <!-- 借助track by $index进行循环-->
<input ng-model="items[key]"/><button ng-click="Fn.del(key)">删除</button>
</div>
{{result}}
<button ng-click="Fn.add()">Add</button>
<button ng-click="getResult()">Result</button>
</body>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有