我有一个非常简单的逻辑,并为此写了条件,我发现逻辑非常好,但它总是给出错误的结果。 我有三个字段即时限制,保留限制和LDC。因此条件是当立即限制和保持一个限制为数值时,它们的总和应小于LDC,并且每个限制的单个值也应小于LDC(当限制值不是数字时,它们返回负数)。
这是我的代码:
immediateLimitError: boolean = false;
hold1LimitError: boolean = false;
ldcLimitError: boolean = false;
sumOflimitVal: any;
changedLdc!: any;
changedImmediateLimit!: any;
changedHold1Limit!: any;
private instantiateValidation(row: any) {
this.newHoldSchedule = this.formBuilder.group({
immediatereleaseForm: [(row.hold1ReleaseAmt != null ? row.hold1ReleaseAmt : ' '), Validators.required],
ldcForm: [(row.ldcAmt != null ? row.ldcAmt : ' ')],
holdOneLimitForm: [(row.hold2ReleaseAmt != null ? row.hold2ReleaseAmt : ' '), Validators.required]
});
this.changedImmediateLimit = row.hold1ReleaseAmt;
this.changedHold1Limit = row.hold2ReleaseAmt;
this.changedLdc = row.ldcAmt;
this.newHoldSchedule.get('immediatereleaseForm')!.valueChanges.subscribe(val => {
this.changedImmediateLimit = val;
if(this.changedHold1Limit >= 0 || val >= 0 ){
if((val + this.changedHold1Limit) > this.changedLdc ||
val > this.changedLdc ||
this.changedHold1Limit > this.changedLdc){
this.immediateLimitError = true;
}
else{
this.immediateLimitError = false;
}
}
});
this.newHoldSchedule.get('holdOneLimitForm')!.valueChanges.subscribe(val => {
this.changedHold1Limit = val;
if(this.changedImmediateLimit >= 0 || val >= 0 ){
if((val + this.changedImmediateLimit) > this.changedLdc ||
val > this.changedLdc ||
this.changedImmediateLimit > this.changedLdc){
this.hold1LimitError = true;
}
else{
this.hold1LimitError = false;
}
}
});
this.newHoldSchedule.get('ldcForm')!.valueChanges.subscribe(val => {
this.changedLdc = val;
if(this.changedImmediateLimit >= 0 || this.changedHold1Limit >= 0 ){
if(val < (this.changedImmediateLimit + this.changedHold1Limit) ||
this.changedHold1Limit > val ||
this.changedImmediateLimit > val){
this.ldcLimitError = true;
}
else{
this.ldcLimitError = false;
}
}
});
}
if(this.immediateLimitError || this.hold1LimitError || this.ldcLimitError){
this.displayError('The sum of Immediate Release limit and Hold 1 Limit exceeds LDC');
event.preventDefault();
}
此代码适用于某些值,并且某些值给出错误的结果。我尝试调试并更改条件,但是我不希望它如何工作。任何帮助,将不胜感激。谢谢
答案 0 :(得分:0)
似乎所有值都是异步分配的,但分别分配。因此,在检查条件之前,无法确保其他值已正确更新。
相反,您可以使用RxJS combineLatest
函数将可观察对象和startWith
运算符组合在一起以提供种子值。
尝试以下
private instantiateValidation(row: any) {
this.newHoldSchedule = this.formBuilder.group({
immediatereleaseForm: [(row.hold1ReleaseAmt != null ? row.hold1ReleaseAmt : ' '), Validators.required],
ldcForm: [(row.ldcAmt != null ? row.ldcAmt : ' ')],
holdOneLimitForm: [(row.hold2ReleaseAmt != null ? row.hold2ReleaseAmt : ' '), Validators.required]
});
combineLatest(
this.newHoldSchedule.get('immediatereleaseForm')!.valueChanges.pipe(startWith(row.hold1ReleaseAmt)),
this.newHoldSchedule.get('holdOneLimitForm')!.valueChanges.pipe(startWith(row.hold2ReleaseAmt)),
this.newHoldSchedule.get('ldcForm')!.valueChanges.pipe(startWith(row.ldcAmt))
).subscribe(
([changedImmediateLimit, changedHold1Limit, changedLdc]) => {
if (
(typeof changedImmediateLimit === "number" && typeof changedHold1Limit === "number") &&
((changedImmediateLimit + changedHold1Limit) < changedLdc) &&
(changedImmediateLimit < changedLdc && changedHold1Limit < changedLdc)
) {
// condition passed
} else {
// condition failed
this.displayError('The sum of Immediate Release limit and Hold 1 Limit exceeds LDC');
event.preventDefault();
}
}
);
}
条件已根据您的问题写成。您可以调整accd。满足您的要求。