angular - 无法访问插值 {{}} 设置的输入字段值
问题描述
我正在开发一个非常简单的应用程序,它包含一个带有 formArray 的表单。在 formArray 中可以选择产品名称和数量。当两者都被选中时,第三个输入字段 -总计- 计算材料的总价格(产品价格 * 金额),使用
<input matInput placeholder="total"
type="number"
formControlName="total"
value="{{getProductPrice(purchaseForm.value.product[i].productId) * purchaseForm.value.product[i].amount}}">
我想对 formArray 中所有项目的总输入字段(价格)求和。该值呈现正常,如果我更改任何产品和/或金额,它会自动更改。
我的问题是,当我尝试访问总输入字段的值时,我得到 0。如果我手动更改值(粘贴、键入等),我可以立即访问它。
我可以访问其他字段(fex。我可以计算所有产品的总和)
问题是因为我设置字段值的方式造成的吗?
有人知道为什么会这样吗?
解决方案
//create an array of totals
totals:number[]=[];
total:number; //<--use a variable to store the total
//After create the form Array
this.yourFormArray.valueChanges(res=>{
let total=0;
res.forEach((x:any,index:number)=>{
//use the "+" to convert to number res.amount;
totals[index]=(+x.amount)*(this.getProductPrice(x.productId));
total+=totals[index]
})
this.total=total;
}
推荐阅读
- reactjs - 节点快速会话反应:服务器cookie未发送到客户端
- google-chrome - “内容下载”在 Chrome 中非常慢,而不是在隐身或其他浏览器中
- wordpress - Wordpress 已注销 Newsletter2Go 小部件中没有任何内容
- listview - QML ListView 委托中的“索引”来自哪里?
- r - ggplot2:在散点图上绘制非标准形状
- javascript - 将 FontAwesome (html) 作为 JQuery var/verb 传递
- r - 在 Rstudio 中使用“str_extract_all”函数(stringr)后未显示 Unicode 字符
- api - DOM 无法在 Vuejs 中呈现属性更改
- vb.net - 是否可以在 PictureBox 中加载 php 结果(回声)?
- sql - “此子查询最多可以返回一条记录” ms access sql中的错误