angular - 如何解决角度条件下的逻辑错误
问题描述
我有一个非常简单的逻辑,我已经写了条件,我发现逻辑非常好,但它总是给出错误的结果。我有三个字段立即限制,持有限制和 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();
}
此代码适用于某些值,而某些值会给出错误的结果。我尝试调试和更改条件,但它无法按照我希望的方式工作。任何帮助,将不胜感激。谢谢
解决方案
似乎所有的值都是异步分配的,但又是分开的。因此,在检查条件之前无法确保其他值已正确更新。
相反,您可以使用 RxJScombineLatest
函数来组合 observables 和startWith
operator 以提供种子值。
尝试以下
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();
}
}
);
}
条件是根据您的问题编写的。你可以调整它。根据您的要求。
推荐阅读
- c# - 如何降低 Unity2D 中的实例化频率
- ruby-on-rails - 在任何来源中都找不到 rake-12.3.1
- c# - C# 相当于在类中覆盖 __dict__
- php - Webcrawler 提取链接元素
- javascript - 在 iE 的浏览器会话中手动添加 cookie
- html - IE11 中的 max-width 和 flex 不起作用
- ruby - 在不显示任何错误消息的情况下保存记录不起作用 - Rails
- angular - (Angular Universal) - firebase.initializeApp 不是函数
- php - 我怎样才能同时抓取两个不同的东西并将其存储在 MySQL 数据库中
- python - 条件映射模块导入