Angular 仅为单击的ID获取JSON数据
我有一组JSON数据。它的底部有2个ID,每个ID中都有进一步的附件数据。我试图点击一个基本ID并在第二页显示它的附件数据。我怎样才能只获取点击ID的信息Angular 仅为单击的ID获取JSON数据,angular,typescript,Angular,Typescript,我有一组JSON数据。它的底部有2个ID,每个ID中都有进一步的附件数据。我试图点击一个基本ID并在第二页显示它的附件数据。我怎样才能只获取点击ID的信息 { "_embedded": { "deliveryMessageList": [ { "id": "73c624c9-6db7-4fd2-ac91-c1084aee0565", "attachments": [
{
"_embedded": {
"deliveryMessageList": [
{
"id": "73c624c9-6db7-4fd2-ac91-c1084aee0565",
"attachments": [
{
"id": "fb1e6d31-4c4e-4356-be5a-827ea5ec422d",
"attachToDeliveryMessage": false
},
{
"id": "160edceb-cda7-483c-a2b9-786f583b523d",
"attachToDeliveryMessage": true
}
]
},
{
"id": "bfd600ad-754f-444d-bddb-f5cf4d7727b8",
"attachments": [
{
"id": "e4454f8c-4ecb-444e-a82a-318bbcee1c11",
"attachToDeliveryMessage": false
},
{
"id": "791a73eb-59cc-4bba-8b16-d442169a7923",
"attachToDeliveryMessage": true
}
]
}
]
}
}
我的UI看起来像这样
我曾尝试向类似这样的单击事件添加ID,但不确定下一步要尝试什么
getCustomerAccountDocs(selectedItem: any, index: number) {
this.CustomerAccountDocsService.getCustomerAccountDocs()
.subscribe((data: any) => {
this.customerAccountDocs = data;
});
}
HTML代码
<tr *ngFor="let item of customerAccountDocs; let i=index" (click)="getCustomerAccountDocs(item, i)">
<td> {{item.accountNumber}}</td>
<td> {{item.id}}</td>
</tr>
{{item.accountNumber}
{{item.id}
听起来,这几乎是一个如何获取特定JSON节点的所有其他同级节点的问题。我不知道你是如何准确地访问ID的(通过一个循环或什么…),但我将简单地向你展示如何在一个循环中访问它。假设上面引用的主要JSON对象存储在一个名为“main”的变量中,然后从那里开始工作:
var embedded = main["_embedded"]["deliveryMessageList"] //or however u access the JSON keys..
var newJSON = {};
embedded.forEach(x => {
for(var k in x) {
var currentIDsiblings = {};
var ID = null;
for(var subkey in x[k]) {
if(subkey != "id") {
currentIDsiblings[subkey] = (x[k][subkey]);
} else {
ID = subkey;
}
}
newJSON[ID] = currentIDsiblings;
}
});
/* now you can access newJSON[myID], to get all of its siblings */
到目前为止你试过什么?请发布。问题编辑您是否只需要获取特定对象/索引的帮助?或者你想把数据传递到第二页,也就是获取对象。我将使用共享服务传递数据。但您的单击处理程序中确实有
selectedItem
。为什么您不能访问selectedItem.attachments
?