如何以角度4显示其id的数据名称

时间:2018-04-05 18:02:05

标签: angular angular-httpclient

示例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;
    });
  }

如何在书籍组件中借助其ID显示作者姓名,但作者组件中存在名称。

1 个答案:

答案 0 :(得分:0)

由于您使用相同的API来获取图书和作者信息,因此您可以使用所需信息创建 BehaviorSubject 并使用 shared service 在书籍和作者组件之间共享数据。

示例代码

export class myervice{
    myData: BehaviorSubject<yourClass> = new BehaviorSubject<yourClass>();
    SaveData() {
        this.myData.next(data);    
    }
}

现在您应该能够通过 two components.

访问myData