如何总结纳克重复里面的动态文本框的值 [英] How to sum a dynamic text box value inside ng-repeat
本文介绍了如何总结纳克重复里面的动态文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建一个动态表单字段里面是有NG重复里面只有一个输入型数字字段,用户可以通过单击添加按钮添加任意数量的字段,现在我需要计算值的动态进入的总和号码字段和更新在文本框中,下面我贴我的code,供大家参考,帮我解决。
<字段集NG重复=$通过指数table.fields田径场> <输入类型=数字NG-模式=table.fields [$指数] .item_count级=表格控NAME =ITEM_COUNTNG-模式=/ ^ [0-9] /占位符=类别项目阅览次数要求> <按钮NG点击=removeChoice()级=删除BTN BTN-危险>关闭< /按钮>
< /字段集>
<按钮类=addfields BTN BTN-成功NG点击=addFormField() ng-disabled=\"!frm.cat_name.$dirty||!frm.cat_desc.$dirty||frm.cat_desc.$invalid||frm.cat_name.$inavalid\">Add田< /按钮>
解决方案
只要创建一个自定义过滤器来概括你的号码:
angular.module(应用)。过滤器('mysum',函数(){
返回功能(项){
变种总和= 0;
items.forEach(函数(项目){
如果(item.item_count){
总和+ = item.item_count;
}
})
返回总和;
}
})
然后用它来显示和:
<跨度> {{table.fields | mysum}}< / SPAN>
I am creating a dynamic form fields in which there is only one input type number field inside the ng-repeat, user can add any number of fields by clicking add button, now I need to calculate the sum of values entered in dynamic number field and update in a text box, below I paste my code for your reference, help me to resolve
<fieldset ng-repeat="field in table.fields track by $index" >
<input type="number" ng-model="table.fields[$index].item_count" class="form-control" name="item_count" ng-pattern="/^[0-9]/" placeholder="Category Item View Count" required>
<button ng-click="removeChoice()" class="remove btn btn-danger" >close</button>
</fieldset>
<button class="addfields btn btn-success" ng-click="addFormField()" ng-disabled="!frm.cat_name.$dirty||!frm.cat_desc.$dirty||frm.cat_desc.$invalid||frm.cat_name.$inavalid">Add fields</button>
解决方案
Just create a custom filter to sum your numbers:
angular.module('App').filter('mysum', function() {
return function(items) {
var sum = 0;
items.forEach(function(item) {
if (item.item_count) {
sum += item.item_count;
}
})
return sum;
}
})
Then use it to display the sum:
<span>{{ table.fields | mysum }}</span>
这篇关于如何总结纳克重复里面的动态文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文