[英]Not able to bind data to view from an object in Angular 2
I have a interface in Angular 2 as follows - 我在Angular 2中有一个接口,如下所示-
export interface ContactInformationModel {
EmailAddress : string;
DirectPhone : string;
Extension : string;
DirectFax : string;
RemoteFax : string;
PersonalEmail : string;
HomePhone : string;
CellPhone : string;
Birthday : string;
Address : string;
PrimaryEmergencyContact : string;
PrimaryEmergencyContactCellPhone : string;
SecondaryEmergencyContact : string;
SecondaryEmergencyContactCellPhone : string;
}
I have a service method in one of my services as follows -- 我在以下一项服务中提供一种服务方法-
public getContactInformation(Id: any): Observable<ContactInformationModel> {
console.log('Service - ContactService - Method - getContactInformation - Params - Id : ' + Id);
this._requestOptions = new RequestOptions({
method: RequestMethod.Post,
url: this.contactInformationUrl,
headers: this.headers,
body: JSON.stringify(Id)
});
console.log('POST Query for Contact Information : ' + JSON.stringify(this._requestOptions));
return this._http.request(new Request(this._requestOptions)).map(this._httpExtractDataService.extractData).catch(this._httpErrorHandlerService.handleError);
}
I am calling the service from my component as follows -- 我正在从组件中调用服务,如下所示:
//Get Contact Information
this._employeeFilesService.getEmployeeContactInformation(this.selectedEmployeeId).subscribe(
data => this.contactDataItem = data,
error => this._errorLoggerService.logError(error, "Contact Information Component"),
() => console.log('Contact Information Received In Information Component -' + JSON.stringify(this.contactDataItem)));
Where contactDataItem is a variable of type ContactInformationModel, declared like this - 其中contactDataItem是ContactInformationModel类型的变量,声明如下:
contactDataItem : ContactInformationModel;
When I run the application, I can see on console that the service is returning an object that looks like follows - 运行该应用程序时,我可以在控制台上看到该服务正在返回一个如下所示的对象-
{
"EmailAddress":"myofficeemail@email.com",
"DirectPhone":"000-000-0000",
"Extension":"0000",
"DirectFax":"000-000-0000",
"RemoteFax":"000-000-0000",
"PersonalEmail":"mypersonalemail@email.com",
"HomePhone":"000-000-0000",
"CellPhone":"000-000-0000",
"Birthday":"2017-05-10",
"Address":"My Address",
"PrimaryEmergencyContact":"Contact Person Name",
"PrimaryEmergencyContactCellPhone":"000-000-0000",
"SecondaryEmergencyContact":"Contact Person Name",
"SecondaryEmergencyContactCellPhone":"000-000-0000"
}
but when I am trying to bind the properties of this object to the of my table, then it is not getting binded. 但是,当我尝试将此对象的属性绑定到表的时,它就没有被绑定。
<tbody>
<tr><td><b>Email Address</b></td><td>{{contactDataItem?.EmailAddress}}</td></tr>
<tr><td><b>Direct Phone</b></td><td>{{contactDataItem?.DirectPhone}}</td></tr>
<tr><td><b>Extension</b></td><td>{{contactDataItem?.Extension}}</td></tr>
<tr><td><b>Direct Fax</b></td><td>{{contactDataItem?.DirectFax}}</td></tr>
</tbody>
Also, please be informed that when I am simply assigning a hardcoded object to the variable contactDataItem and binding it to the then the binding is successfull. 另外,请注意,当我只是将硬编码对象分配给变量contactDataItem并将其绑定到时,绑定就成功了。 What am I doing wrong ?
我究竟做错了什么 ? Any help would be appreciated.
任何帮助,将不胜感激。
Thanks, Amit Anand 谢谢阿米特·阿南德
You could try accessing the data property. 您可以尝试访问data属性。
getMovie(id: number): Observable<IMovie> {
const url = `${this.moviesUrl}/${id}`;
return this.http.get(url)
.map((res: Response) => {
const body = res.json();
return <IMovie>body.data || {};
})
.do(data => console.log(JSON.stringify(data)))
.catch(this.handleError);
}
From the Angular docs: 从Angular文档中:
Don't expect the decoded JSON to be the heroes array directly.
不要期望解码后的JSON直接成为heroes数组。 This server always wraps JSON results in an object with a data property.
该服务器始终将JSON结果包装在具有data属性的对象中。 You have to unwrap it to get the heroes.
您必须打开包装才能获得英雄。 This is conventional web API behavior, driven by security concerns.
这是传统的Web API行为,受安全问题的驱动。
https://angular.io/docs/ts/latest/guide/server-communication.html https://angular.io/docs/ts/latest/guide/server-communication.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.