Я пытаюсь получить данные из API с помощью Angular2 и привязать их к просмотру. После выполнения запроса мой компонент получает только объект массива [object Object],[object Object],[object Object]. Могу ли я получить помощь по извлечению объекта, чтобы "*ngFor" мог прочитать каждый объект. Я хочу иметь возможность привязывать данные для просмотра.
**This is my API**
[{
_id: "578c8c670f3cd0941efb337c",
name: "John",
gender: "Male",
age: "34",
__v: 0,
},
{
_id: "5794a90f96391dcc2436e43f",
name: "Valur",
gender: "Male",
age: "22",
__v: 0,
},
{
_id: "5794abad70df28541eab170b",
name: "Paul",
gender: "Male",
age: "43",
__v: 0,
}]
UserService похож на:
import { Http } from 'angular2/http';
constructor(private _http: Http) { }
getuser(): Observable<any> {
return this._http.get('http://localhost:1000/api/users')
.map(res => res.json());
}
Это мой угловой со службой, которая обрабатывает запрос.
import { UserService } from '../services/userservice';
constructor(private userService: UserService) { }
_data: Object;
getUser():void {
this.userService.getuser()
.subscribe(data => this._data = data,
error => console.log(error));
console.log(this._data); //this part doesn't pull data at all.
}
//My view
<div *ngFor="#u of _data">
{{u.name}} //this part gives me an error
</div>
//But when i try to pull data from "subscribe" like this:
<div>
{{_data | json }} //data is available
</div>
{{_data | json}}
? - person Günter Zöchbauer   schedule 25.07.2016<div *ngFor="let u of _data">
. - person Günter Zöchbauer   schedule 25.07.2016