【发布时间】:2018-09-26 02:36:55
【问题描述】:
我有一个服务器响应,由一个对象和对象数组组成,如下所示:
{
"bookNumber": "123214",
"Desc": "book desc",
"title": "title here",
"published": "12/01/2016",
"author": {
"authorname": "VictorA"
},
"Category": [{
"genere": "Type1",
"number": "2331",
"sesction": "Fiction"
}, {
"genere": "type2",
"number": "430359",
"sesction": "Kids"
}, {
"genere": "type2",
"number": "436430",
"sesction": "Kids"
}, {
"genere": "type2",
"number": "123914",
"sesction": "Kids"
}],
"Publisher": [{
"name": "Pubbook",
"pubId": "81.25402-0233",
"lastModified": "2012-02-09"
}]
}
在组件上,我从服务订阅 Observable,如下所示:
this.myService.getDetails(id).subscribe (data => {
this.resp = data;
console.log (JSON.stringify(this.resp));
})
在 HTML 中:
我使用 *ngFor 来循环响应如下:
<div class="panel-group" id="accordion">
<div class="panel panel-default" id="panel1">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-target="#collapseOne"
href="#collapseOne">
Book
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body"></div>
{{bookNumber}}
...so on
</div>
</div>
<div class="panel panel-default" id="panel2">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-target="#collapseTwo"
href="#collapseTwo" class="collapsed">
Documents
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body" *ngFor = "let item of resp.Category"></div>
// here How can I extract items based on genere type e.g. Type1 <div col="md-6"> {{items of Type1 genere}} {{}}</div>
// Type2 <div col="md-6">{{items of Type2 genere}}</div>
I want to display details of each Genere in separately and update on selection for e.g. when i click Number 2331 get details of that object, and same for 4302359
</div>
</div>
</div>
如何根据Genere Type(1,2...等)循环Category数组,并在用户点击数字后更新每个项目的详细信息?
【问题讨论】:
-
尝试将来自您服务器的数据转换为适合您的数据结构。
-
@AnisTissaoui,你能举个例子吗? :)
-
编辑帖子并提供更多解释。我不明白您为什么订阅并获取数据,然后将其分配给 this.resp,但随后您记录了我一无所知的 this.items。
-
谢谢,我编辑了console.log,它是错误的...我订阅了observable,然后将它分配给this.resp以迭代对象
-
你提供的对象是服务器的完整响应吗?还是其中的一部分?
标签: angular