微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果条件成角度,如何解决逻辑错误

如何解决如果条件成角度,如何解决逻辑错误

我有一个非常简单的逻辑,并为此写了条件,我发现逻辑非常好,但它总是给出错误的结果。 我有三个字段即时限制,保留限制和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();
                }

代码适用于某些值,并且某些值给出错误的结果。我尝试调试并更改条件,但是我不希望它如何工作。任何帮助,将不胜感激。谢谢

解决方法

似乎所有值都是异步分配的,但分别分配。因此,在检查条件之前,无法确保其他值已正确更新。

相反,您可以使用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。满足您的要求。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。