服务从后端获取json数据:
constructor(private http: Http) { };
getUsers(): Observable<any> {
return this.http.get('http://127.0.0.1:8000/app_todo2/users_list');
};
在组件中,处理此数据:
ngOnInit() {
this.getAllUsersData();
}
private getAllUsersData(): void {
this.usersService.getUsers().subscribe(
data => {
this.allUsersData = JSON.parse(data.json());
console.log(this.allUsersData);
})
};
但我需要在服务中处理这些数据。我需要获得组件清晰的数据:
private getAllUsersData(): void {
this.usersService.getUsers().subscribe(
data => {
this.allUsersData = data;
console.log(this.allUsersData);
})
};
请帮帮我。
答案 0 :(得分:0)
映射您的服务数据:
getUsers(): Observable<any> {
return this.http.get('http://127.0.0.1:8000/app_todo2/users_list')
.map(data => data.json());
}
然后:
private getAllUsersData(): void {
this.usersService.getUsers().subscribe(
data => {
this.allUsersData = data;
console.log(this.allUsersData);
})
};