我只是试图显示一些信息,我通过服务进入了我的API,但在控制台显示我需要的信息时,我无法在我的视图中访问它。Angular2显示数据
我的角服务
getById(id){
return new Promise((resolve, reject) => {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.get(this.urlRoot+'entreprise/'+id, {headers:headers})
.subscribe(res=>{
let data = res.json();
resolve(data.entreprise);
}, (err) =>{
reject(err)
})
})
}
我的视图代码示例
<div class="entreprise-name"><h1> {{ entreprise.name }} </h1></div>
错误代码
Cannot read property 'id' of undefined
的API JSON的answere
{
"entreprise": {
"id": 1,
"domaine_id": 1,
"category_id": 1,
"name": "Culry Hairs",
"location": "Rue du Travail 1, 7000 Mons, Belgium",
"description": "La coupe qui décoiffe",
"contact_person": "Marie Kristy",
"phone_number": "065/53.46.55",
"createdAt": "2017-03-05T23:00:00.000Z",
"updatedAt": "2017-03-05T23:00:00.000Z"
}
}
img of the 'console.log' of the data
可能的重复[我无法在页面上获取JSON数据在Ionic 2插值后](http://stackoverflow.com/questions/42630452/im-unable-to-get-json-data-on - 插页后插入在离子-2) – echonax
对象将是undefined请将* ngIf =“entreprise”添加到div –