简体   繁体   English

服务器调用之后在分配之前使用值

[英]value is using before it assigns after the server call

value is using before it assigns after the server call 服务器调用之后在分配之前使用值

/** * Consume patient info from cas service. / ** *从cas服务获取患者信息。 */ * /

 getPatientInfo(event: any, affectType:string) {
    this.patientInfo= new PatientModel();
    let headers = new Headers();
    let estCode = localStorage.getItem(AppUtils.DEFAULT_INSTITUTE); // Logged in user default institute
    headers.append('Content-Type', 'application/json');
    this.masterDataService.getPatientInfo(event).subscribe(result =>{this.patientInfo = result,   console.log("1 "+JSON.stringify(this.patientInfo));
    });
    console.log("2"+JSON.stringify(this.patientInfo));
    this.addPerson(affectType);
}

Here console message 1 displays the result, where messgae 2 return {} string. 此处控制台消息1显示结果,消息2返回{}字符串。

console message is like 控制台消息就像

2 {}
1 {"address":null,"categoryCode":0}

How can I make the statement following the serer call to wait in angular2 我如何在serer调用后在angular2中等待语句

since javascript is asynchronous it does not wait until the observable subscribed to an event. 由于javascript是异步的,因此它不会等到观察到的事件已订阅。 it keeps on executing. 它继续执行。 that is why 2nd console get printed first. 这就是为什么第二个控制台先打印的原因。

one thing you can do is create a function and call it inside the subscribe so that it will execute after the result is resolved. 您可以做的一件事是创建一个函数,并在订阅中调用它,以便在解析结果后执行该函数。

getPatientInfo(event: any, affectType:string) {
      this.patientInfo= new PatientModel();
      let headers = new Headers();
      let estCode = localStorage.getItem(AppUtils.DEFAULT_INSTITUTE); // Logged in user default institute
      headers.append('Content-Type', 'application/json');

      this.masterDataService.getPatientInfo(event).subscribe(result =>{this.patientInfo = result,   console.log("1 "+JSON.stringify(this.patientInfo));
        console.log("2"+JSON.stringify(this.patientInfo));
        this.addPerson(affectType);
      });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM