[Angular] Subscribing to the valueChanges Observable
2024-08-23 13:33:48
For example we have built a form:
form = this.fb.group({
store: this.fb.group({
branch: '',
code: ''
}),
selector: this.createStock({}),
stock: this.fb.array([])
});
We want to reponse to each time 'stock' value changes.
To do that we can subscrube 'valueChanges' for form.
Notice that, 'valueChanges' is an Observable, you need to subscribe to it, And it not only exists for 'form', also for 'formControl, formGroup, formArray':
this.form.get('stock')
.valueChanges
.subscribe(...)
If you want to give an initial value, you can use 'startWith' from 'rxjs/add/opreator/startWith'.
this.form.get('stock')
.valueChanges
.startWith(this.form.get('stock').value)
.subscribe((stocks) => {
this.total = this.calculateTotal(stocks);
}) calculateTotal(stocks: Item[]): number {
return stocks.reduce((acc, next) => {
return acc + (Number(next.quantity) * Number(this.productMap.get(next.product_id).price))
}, )
}
最新文章
- in addition to 和 except for
- mui slider 改变默认index
- 6月辞职->;帝都生活
- preventDefault()方法
- spring Integration服务总线
- 【Android车载系统 News | Tech 1】News 谷歌开发车载Android系统 2014-12-19
- Object-C - 类的定义
- ARM GCC CodeSourcery 下载地址
- Js监控回车事件
- iOS_25彩票_幸运转盘
- python在windows和linux环境的进程对比
- JAVAscript学习笔记 js事件 第一节 (原创) 参考js使用表
- two sum --无脑法
- This Android SDK requires Android Developer Toolkit version 17.0.0 or above. Current version is 10.0.0.v201102162101-104271. Please update ADT to the latest version.
- SmileyCount.java笑脸加法程序代写(QQ:928900200)
- 杭电ACM 1297 Children’s Queue
- 升级my.cnf注意
- 使用Prometheus+Grafana监控MySQL实践
- CSS之Header--我的头部我做主
- Azkaban学习笔记(一)