我有2个角反应型形式的字段,当我改变%增益时,价格立即改变,而当价格改变时,%就会被计算并立即改变。
我做了以下事情:
const avgPrice = this.totalStockProductForm.get('AVERAGE_PRICE').value;
this.totalStockProductForm.get('PERCENTAGE_GAIN').valueChanges.subscribe(percentage => {
this.totalStockProductForm.controls['FINAL_PRODUCT_PRICE'].patchValue(
avgPrice + avgPrice * (percentage / 100)
);
});
this.totalStockProductForm.get('FINAL_PRODUCT_PRICE').statusChanges.subscribe(finalPrice => {
this.totalStockProductForm.controls['PERCENTAGE_GAIN'].patchValue(
((finalPrice - avgPrice) / avgPrice) * 100
);
});
我知道,它最终发生了,这将导致无限循环。如何实现更改一个条目并立即预览另一个条目的目标?
答案 0 :(得分:1)
const avgPrice = this.totalStockProductForm.get('AVERAGE_PRICE').value;
this.totalStockProductForm.get('PERCENTAGE_GAIN').valueChanges.subscribe(percentage => {
this.totalStockProductForm.controls['FINAL_PRODUCT_PRICE'].patchValue(
avgPrice + avgPrice * (percentage / 100),
{ emitEvent: false }
);
});
this.totalStockProductForm.get('FINAL_PRODUCT_PRICE').statusChanges.subscribe(finalPrice => {
this.totalStockProductForm.controls['PERCENTAGE_GAIN'].patchValue(
((finalPrice - avgPrice) / avgPrice) * 100,
{ emitEvent: false }
);
});
添加{embedEvent:false}将阻止其他字段上其他事件的检测,从而解决了无限循环问题!希望它能对某人有所帮助!