Angular订阅值始终被赋予值-1,即实际值之前的值

时间:2018-12-17 16:07:37

标签: angular https angular6 angular-services subscribe

.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

1 个答案:

答案 0 :(得分: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 = "";
        })
}