angular - 计算变化时所有角度反应输入的总量
问题描述
我有反应形式,只有多个数字输入。我使用函数来修改更改时的每个输入值:
initsForm() {
this.form = this.fb.group({
coffee: ['0', [
Validators.min(0)
]],
tea: ['0', [
Validators.min(0)
]]
})
this.countAmount();
}
countAmount() {
this.form.valueChanges.subscribe(val => {
Object.keys(this.form.controls).forEach(key => {
console.log((this.form.get(key).value * 1.75))
})
})
}
此控制台记录更改后每个输入的修改值,但我需要控制台记录所有输入的总量。我怎样才能做到这一点?
解决方案
我应该使用reduce
方法:
countAmount() {
this.form.valueChanges.subscribe(val => {
let res = [];
Object.keys(this.form.controls).forEach(key => {
res.push(this.form.get(key).value * this.counters[key])
});
res = res.reduce((prev, next) => prev + next);
this.totalAmount += Number(res);
})
}
推荐阅读
- javascript - ajax发布后重定向到url不起作用
- java - 如何将svg中的前景色更改为png转换?
- javascript - 选择输入单选时更改最近类的样式 - Javascript
- java - 查找具有不同值的 CSS 选择器的问题
- php - 最大文件大小的 Laravel 验证规则不起作用
- scala - Scala中使用jdk10的不确定数组复制
- c# - 标签不填充
- django - 会话的 Django 设置是什么?
- terraform - 我可以在 TerraForm main.tf 文件中使用变量吗?
- xcode - 无法让 Xcode 将 libsteam_api.dylib 与构建捆绑在一起