【发布时间】:2019-06-16 20:04:50
【问题描述】:
目标:
从 json 中获取数据到“test: ITest[];”然后使用console.log显示
问题:
代码不起作用。我错过了什么?
信息:
我是 Angular 的新手
谢谢!
服务
getData2(): Observable<ITest[]> {
return this.http.get<ITest[]>('https://api.github.com/users/hadley/orgs');
}
export interface ITest {
login: string,
node_id: string
}
组件
public test: ITest[];
ngOnInit() {
this.getData2()
}
getData2() {
this.asdfService.getData2().subscribe(res => { this.test = res });
console.log(this.test)
};
export interface ITest {
login: string,
node_id: string
}
【问题讨论】:
-
你能显示组件的完整代码吗? (不是 HTML,只是类)
-
另外,当您说
The code doesn't work.时,发生了什么?有编译错误吗?运行时错误?没有视觉反馈? -
将您的console.log() 移入 传递给subscribe() 的回调函数。 http 请求是异步的。您不能指望在您发送请求后立即获得响应。它只会在以后可用,然后会调用回调。
标签: angular visual-studio-code