1
我错过了插值的东西,我想。 我使用http.get为我的服务来获得数据Angular 4从Http获取json并显示到Dom
post.service代码:
getPosts() {return this._http.get(this._Url + 'events').map((_response: Response) => {
const data = _response.json()
return _response.json();
});
我的组件i上运行下面的代码传递给res数组 分量码的响应:
getdata(){this._postService.getPosts()
.subscribe(
posts => {
posts.forEach(posts => {
this.res.push(
new event(
posts.field_event_title[0].value,
posts.field_event_price[0].value,
"test loc"
)
)
});
}
,
(error) => console.log(error, "error from observable")
);
console.log(this.res, "res after subs")
}
HTML代码:
<div class="row" *ngFor="let post of res">
Title: {{post.title}} <br>
Price: {{post.price}} <br>
Location:{{post.location}}
</div>
我的问题是与* ngFor我得到我的数据显示。但我想只读1后,前。 RES [0] .title伪。当我在html {{res [0] .title}}中使用时,我得到错误“无法读取属性”标题“的未定义。任何帮助!?最后我想要的是能够使用数组水库在DOM
因为你的阵列开始空,你需要用'* ngIf =“res.length将其包围> 0“' – Ploppy
试试'res [0]?title'这是异步问题,所以请使用安全导航操作符:) – Alex
[Angular 2模板标签说对象未定义]的可能重复(http://stackoverflow.com/questions/35073267/angular-2-template-tags-say-object-is-undefined) – Alex