如何在 angular 中显示带有 id 的数据名称 4
how to display data name with its id in angular 4
样本json:
{
name:java, //this is from book component
language:english,//this is from book component
author_id:120130930930, //this is from Author component
}
book.comp.html
<ul *ngFor="let book of books" class="list-group-item">
<li>{{book.name}}</li> //displaying
<li>{{book.language}}</li>
<li>{{book.authorId}}</li>
<li>{{book.author.name}} -->//here i need to display author name with help of its id in book component but name is present in author component.
servicestorage.ts
getBooks() {
return this.http.get('http:myurl:3000/books').map(data => {
return data;
});
}
getAuthors() {
return this.http.get('http:myurl:3000/authors').map(data => {
return data;
});
}
如何在 book 组件中借助其 id 显示作者姓名,但名称存在于作者组件中。
由于您使用相同的 api 来获取图书和作者信息,因此您可以创建包含所需信息的 BehaviorSubject
并使用 shared service
在图书和作者组件之间共享数据。
示例代码
export class myervice{
myData: BehaviorSubject<yourClass> = new BehaviorSubject<yourClass>();
SaveData() {
this.myData.next(data);
}
}
现在您应该可以跨
访问 myData
样本json:
{
name:java, //this is from book component
language:english,//this is from book component
author_id:120130930930, //this is from Author component
}
book.comp.html
<ul *ngFor="let book of books" class="list-group-item">
<li>{{book.name}}</li> //displaying
<li>{{book.language}}</li>
<li>{{book.authorId}}</li>
<li>{{book.author.name}} -->//here i need to display author name with help of its id in book component but name is present in author component.
servicestorage.ts
getBooks() {
return this.http.get('http:myurl:3000/books').map(data => {
return data;
});
}
getAuthors() {
return this.http.get('http:myurl:3000/authors').map(data => {
return data;
});
}
如何在 book 组件中借助其 id 显示作者姓名,但名称存在于作者组件中。
由于您使用相同的 api 来获取图书和作者信息,因此您可以创建包含所需信息的 BehaviorSubject
并使用 shared service
在图书和作者组件之间共享数据。
示例代码
export class myervice{
myData: BehaviorSubject<yourClass> = new BehaviorSubject<yourClass>();
SaveData() {
this.myData.next(data);
}
}
现在您应该可以跨