在创建可观察时间时评估订阅回调 [英] Evaluate subscription callback at time observable is created
问题描述
我有一个反应式表单,在该表单中,当可观察对象发出数据(并调用订阅函数)时,以下订阅中的函数对itm["value"].length-1
进行求值.
I have a Reactive Form in which it appears the function inside the following subscription evaluates itm["value"].length-1
at the time the observable emits data (and calls the subscription function).
this.formCtls[controlName] = new FormControl('', {updateOn: 'blur'});
this.userForm.addControl(controlName, this.formCtls[controlName]);
this.formCtls[controlName].valueChanges.subscribe(val=>{
itm["value"][itm["value"].length-1]=val;
this.renderDataArray();
});
但是,我希望在创建observable/(FormControl)时对订阅回调函数表达式itm["value"].length-1
进行求值.
However I want the subscription callback function expression itm["value"].length-1
to be evaluated at the time the observable/(FormControl) is created.
例如,在创建表单控件时,itm["value"].length
可能仅是2
,但是在Observable发出itm["value"].length
时,可能是6
或7
或任何其他数字.我如何(以编程方式)将2
(或等同版本)硬连接"到订阅回调中?
For instance, at the time the form control is created, itm["value"].length
might only be 2
, but at the time the Observable emits itm["value"].length
may be 6
or 7
or any other number. How can I (programatically) "hardwire" the 2
(or equivalent) into the subscription callback?
非常感谢!
推荐答案
我现在不太确定下面的答案.它在我的代码中有效,但是我不确定为什么起作用.我将重新评估此临时答案并发布更新.同时,我非常感谢专家的任何意见,因为我对这些Observable感到有些困惑和迷茫. . .
I'm not so sure about the answer below now. It worked in my code, but I'm not sure why it worked. I will re-evaluate this provisional answer and post an update. In the meantime, I'd be very grateful for any input from an expert as I'm a bit confused and lost amongst these Observables. . .
this.formCtls[controlName] = new FormControl('', {updateOn: 'blur'});
this.userForm.addControl(controlName, this.formCtls[controlName]);
const idx = itm["value"].length-1;
this.formCtls[controlName].valueChanges.subscribe(val=>{
itm["value"][idx]=val;
this.renderDataArray();
});
这篇关于在创建可观察时间时评估订阅回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!