我是 Angular 4 的新手,不知道如何获得我的解决方案。 我从 mongodb 获取数据,它工作正常。
contact.company 字段中是公司的 _id。公司名称保存在company.name中。
而不是{{contact.company}}(存储id的地方)应该有client.name(这是公司的名称)来显示真实姓名,而不是id。我如何获得它?
我想我需要自动调用一个方法,从数据库中获取值?
完整代码在这里https://github.com/hevilp/angular/blob/master/client/app/contact/contact.component.html
完整代码在这里:https://github.com/hevilp/angular
<tr *ngFor="let contact of contacts">
<td>{{contact.anrede}}</td>
<td>{{contact.titel}}</td>
<td>{{contact.vorname}}</td>
<td>{{contact.nachname}}</td>
<td>{{contact.company}}</td>
<td>
和 contact.component.ts(代码不工作...)
getClient() {
this.clientService.getClient().subscribe(
data => this.client = data,
error => console.log(error),
() => this.isLoading = false
);
}
最佳答案
我不确定我理解你的问题,告诉我我理解错了。 你想显示联系人数据,但是对于字段 contact.company,你想显示从数据库中获取的真实姓名,在 id 的函数中:contact.company,使用函数: 获取客户(联系人) ?
如果是这样,为什么要等到显示数据后才能得到公司的真实名称?你为什么不解析你的联系人数组并添加一个字段:contact_name 从一开始,我的意思是在 ngOnInit 中?
如果是这样,您应该将此调用添加到 getContacts()
getContacts() {
this.contactService.getContacts().subscribe(
data => {
for (let contact of data) {
contact.contact_name = this.getClient(contact);
}
this.contacts = data
},
error => console.log(error),
() => this.isLoading = false
);
}
这将确保从头开始加载所有数据。
希望对你有所帮助。如果我弄错了或者您正在寻找其他解决方案,请告诉我。
关于javascript - Angular 4 平均堆栈 : How to get a value depending on another {{value}},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46961238/