Javascript 如何折叠为在angular中具有*ngFor指令的accordion json对象details?

Javascript 如何折叠为在angular中具有*ngFor指令的accordion json对象details?,javascript,html,json,angular,ngfor,Javascript,Html,Json,Angular,Ngfor,我想要像accordion一样折叠和扩展我的json对象细节。我已经使用了for循环和*ngFor指令 toggleCollapse(){ this.isCollapsed=!this.isCollapsed; } {{t.title}} {{t.description}} {{t.content}} 阅读更多 少读 @Ruwan,创意(no.ts) 我们添加了一个新属性“isCollapsed”,以使响应变为 [ {imgUrl:"",title:"",description:"",c

我想要像accordion一样折叠和扩展我的json对象细节。我已经使用了for循环和*ngFor指令

toggleCollapse(){
this.isCollapsed=!this.isCollapsed;
}

{{t.title}}
{{t.description}}
{{t.content}}
阅读更多
少读
@Ruwan,创意(no.ts)

我们添加了一个新属性“isCollapsed”,以使响应变为

[
  {imgUrl:"",title:"",description:"",content:"",isCollapsed:true},  
  {imgUrl:"",title:"",description:"",content:"",isCollapsed:false},
  ...
]
我们如何添加这个“额外”属性

无需修改.ts即可工作,因为您有一个对象数组,可以添加一个属性directy(如我们所做的那样,在.html中)。但您更希望在.ts中添加此属性。在这种情况下,你唯一需要的就是

//I supouse you have some like
   this.httpClient.get(...).subscribe(response=>{this.response=response})
//replace the line before to transform the response using "map" and spread operator
   this.httpClient.get(...).subscribe(response=>{
        this.response={
           ...response, //all properties of response
                        //but articles we add a property isCollapsed
           articles:response.articles.map(a=>{
               ...a,
               isCollapsed:true
           })
   })

如果要折叠多个“div”,则需要多个变量。例如,你可以在文章中添加一个变量,并使用t.iscollapsed,而不是collapsed。请看,你能解释一下吗?我试过了。但我仍然有问题。请看我的答案(和注释)
[
  {imgUrl:"",title:"",description:"",content:"",isCollapsed:true},  
  {imgUrl:"",title:"",description:"",content:"",isCollapsed:false},
  ...
]
//I supouse you have some like
   this.httpClient.get(...).subscribe(response=>{this.response=response})
//replace the line before to transform the response using "map" and spread operator
   this.httpClient.get(...).subscribe(response=>{
        this.response={
           ...response, //all properties of response
                        //but articles we add a property isCollapsed
           articles:response.articles.map(a=>{
               ...a,
               isCollapsed:true
           })
   })