【发布时间】:2017-12-18 12:33:44
【问题描述】:
我正在尝试从 http.get() 获取值并将其分配给局部变量。
这是我的代码。 用户名.ts
import { Http } from '@angular/http';
this.http.get('http://localhost:8000/user/').subscribe(res => {
this.username$ = res;
console.log(this.username$)}) //working
console.log(this.username$)
我得到 undefined 作为输出
http 调用 - http://localhost:8000/user/ 将只返回一个用户名。我必须将它存储在一个变量中。并使用它来调用另一个 http 调用
return this.http.get(`http://localhost:8001/getdettails/${this.username$}/${experimentPath}`)
请帮我解决这个问题。谢谢。
【问题讨论】:
-
你了解异步代码和回调的工作原理吗?你的代码的最后一行在你的 observable 返回之前执行。所以还不能设置变量。
-
@Pac0 删除了标志,因为我错过了问题的最后一部分(http 调用链)
标签: angular