【发布时间】:2019-01-13 09:45:55
【问题描述】:
我对 observable 的工作原理有了基本的了解,我可以通过调用 subscribe 方法获取数据并在我的模板中呈现数据。但我无法枚举组件中可观察对象返回的数据。我想在将数据发送到我的模板之前在组件级别处理我的数据。
到目前为止我已经尝试过了:
我的服务:
getCountries(): Observable<IUser> {
return this.http.get<IUser>('https://jsonplaceholder.typicode.com/posts');
}
我在组件中的 ngInit 方法:
ngOnInit() {
this.countryService.getCountries().subscribe((users => {
this.userJson = users;
}));
console.log(this.userJson); // Showing undefined
//giving exception while calling length property
for (var i = 0; i < this.userJson.length; i++) {
}
}
不确定如何处理?
【问题讨论】:
标签: angular observable angular2-services angularjs-components