angular - 在模板中使用表达式作为输入值
问题描述
<input
ngModel
name='quantity'
placeholder='Quantity'
type="number"
class="form-control"
pattern="^\d*(\.\d{0,2})?$"
required />
<input
ngModel
name='price'
class="form-control"
placeholder='Price'
type="number"
required />
<input
name='total'
class="form-control"
type="number"
[value]='addRowForm.value.quantity * addRowForm.value.price' />
我有三个输入字段Quantity、Price、Total,数量和价格的乘积应该显示在 Total 字段中,我可以在 Typescript 代码中执行此操作,但我想知道是否有一个简单的解决方案可以仅在模板中执行此操作。
我已经尝试过了,但它不起作用。
[value]='addRowForm.value.quantity * addRowForm.value.price'
解决方案
[(ngModel)]
在字段中添加。
试试这样:
<input [(ngModel)]="quantity" name='quantity' placeholder='Quantity' type="number" class="form-control" pattern="^\d*(\.\d{0,2})?$" required />
<input [(ngModel)]="price" name='price' class="form-control" placeholder='Price' type="number" required />
<input name='total' class="form-control" type="number" [(ngModel)]='quantity * price' />
你也可以像这样使用它:
TS:
newRowDetails = {}
模板:
<input [(ngModel)]="newRowDetails.quantity" name='quantity' placeholder='Quantity' type="number" class="form-control" pattern="^\d*(\.\d{0,2})?$" required />
<input [(ngModel)]="newRowDetails.price" name='price' class="form-control" placeholder='Price' type="number" required />
<input name='total' class="form-control" type="number" [(ngModel)]='newRowDetails.quantity * newRowDetails.price' />
推荐阅读
- android - 切换到新页面时使用抽屉时出现颤振错误
- javascript - 如果我调用 Just Object,则获取特定属性而不是 Object
- intellij-plugin - Intellij 自定义插件不起作用它说清单丢失或无法识别主类
- r - 对具有分组数据的 purrr map() 中的特定列使用 dplyr summarise()
- python - 为什么 fetchone() 在这里没有返回?
- ios - http请求在iOS上被冻结
- laravel - 上传 20 张图片而不是 30 张
- javascript - react native:解决键盘在文本框上方并且看不到正在写什么的问题的方法是什么?
- acumatica - 如何更改选择器描述字段的显示名称
- reactjs - 在外部窗口中渲染 React 组件时传递文档