angular - 角度订阅值总是被赋予值 - 1,即真实值之前的值
问题描述
我正在尝试在.subscribe
完成后执行一个事件,因为它取决于结果之后的代码
verifBordereauExistBase(id: string) {
return this._BourdereauService.get_one(id).subscribe(
data = > {
if (data.idBourdereau == null) {
this.idBourdereauIsValid = false;
} else {
this.idBourdereauIsValid = true;
}
}, err = > {
console.error(err)
}, () = > {})
}
主要测试在这里
AddBordereautoList() {
this.verifBordereauExistBase(this.idbordereaux);
console.log(this.idBourdereauIsValid)
if (Number.isNaN(Number(this.idbordereaux))) {
this.notifier.notify('error', 'Format code a bare invalide');
} else if (this.idbordereaux.length != 10) {
this.notifier.notify('error', 'Format code a bare invalide');
} else if (this.idBourdereauIsValid == false) {
this.notifier.notify('error', 'Bordereau n\'existe pas ');
} else {
if (this.map.size == 0) {
this.map.set(1, this.idbordereaux);
} else {
let x: number = this.verifexistbordereau(this.idbordereaux);
if (x === 0) {
this.map.set(this.map.size + 1, this.idbordereaux);
} else {
this.notifier.notify('error', 'Bordereau N°' + this.idbordereaux + 'existe deja dans la liste!');
}
}
}
this.idbordereaux = "";
}
我正在执行此代码,而 的值this.idBourdereauIsValid
落后了一步,它给了我值 - 1 总是
解决方案
this.verifBordereauExistBase(this.idbordereaux);
可能正在执行的代码之前this.verifBordereauExistBase(this.idbordereaux);
有机会返回。为确保这样做,请将AddBordereautoList()
代码添加到.subscribe
完整的步骤中:
verifBordereauExistBase(id: string) {
return this._BourdereauService.get_one(id).subscribe(
data = > {
if (data.idBourdereau == null) {
this.idBourdereauIsValid = false;
} else {
this.idBourdereauIsValid = true;
}
}, err = > {
console.error(err)
}, () = > {
console.log(this.idBourdereauIsValid)
if (Number.isNaN(Number(this.idbordereaux))) {
this.notifier.notify('error', 'Format code a bare invalide');
} else if (this.idbordereaux.length != 10) {
this.notifier.notify('error', 'Format code a bare invalide');
} else if (this.idBourdereauIsValid == false) {
this.notifier.notify('error', 'Bordereau n\'existe pas ');
} else {
if (this.map.size == 0) {
this.map.set(1, this.idbordereaux);
} else {
let x: number = this.verifexistbordereau(this.idbordereaux);
if (x === 0) {
this.map.set(this.map.size + 1, this.idbordereaux);
} else {
this.notifier.notify('error', 'Bordereau N°' + this.idbordereaux + 'existe deja dans la liste!');
}
}
}
this.idbordereaux = "";
})
}
推荐阅读
- xamarin - 使用 CreateContent() 创建的数据模板中的 Xamarin 自定义控件
- python - 每次用户在 Flask 上登录我的应用程序时如何为他们创建一个表?该表将存储他们输入的详细信息并专门向他们显示
- node.js - 如果另一个目录中的文件被更改,是否有一个命令允许您重新启动 pm2 应用程序?
- c# - 如何从被测函数中实例化的对象替换函数调用
- jasper-reports - Jasper 报告预览中元素的奇怪定位
- c++ - 包含 gtest.h 如何打破标准算法的模板参数推导?
- python - for循环中的Python If语句-列表索引超出范围
- sql - 如何使用具有相同外键的其他行的值更新行
- mongodb - 将 DigitalOcean Managed MongoDB 连接到 Laravel(连接错误调用 ismaster)
- redis - Redis java地图分页