[英]Dynamic parameters startWith Rxjs
我希望在值更改時添加多個輸入字段的發射值。
問題是:有多個字段,我希望它是動態的,因為我不知道我可以提前擁有多少個字段!
// In this case i have 3 input fields
var keyObjectFields = ["0", "1", "2"];
const observedValues = keyObjectFields.map(key => this.credentialsForm.controls[key].valueChanges
.map(value => +value).startWith(0, 0, 0))
const resSurface = combineLatest(observedValues)
.pipe(map(([value0, value1, value2]) => { return value0 + value1 + value2 }));
resSurface.subscribe(val => { this.surface = val });
使用startWith(0)
開始觀察到的每個具有0。使用array.reduce
來計算陣列的總和。
// In this case i have 3 input fields
var keyObjectFields = ["0", "1", "2"];
const observedValues = keyObjectFields.map(
key => this.credentialsForm.controls[key].valueChanges.pipe(
map(value => +value),
startWith(0)
)
);
const resSurface = combineLatest(observedValues).pipe(
map(values => values.reduce((sum, curr) => sum + curr))
);
resSurface.subscribe(val => { this.surface = val });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.