2017-06-15 126 views
0

处理多个分量I有这样的问题:可以折叠

<ng-container *ngFor="let item of list"> 
    <item [content]="item.content" [title]="item.title" [open]="true" (toggleclicked)="/*triggered when a user wants to fold/unfold*/"></item> 
</ng-container> 

[打开]输入是指示如果我的组件应该被“折叠”或“扩展”是布尔值。是否有一种简单的方法来在ngFor中像这样管理多个组件?

如果我有一个内部状态并自行管理折叠/展开,这是一种更好的做法吗?

+0

我想我可以理解你想要什么。为什么不使用[Bootstrap可折叠项目](http://getbootstrap.com/javascript/#collapse)? – trichetriche

+0

我对bootstrap了解不多,而且我还需要能够拥有诸如toggleAll()的控件 – HaneTV

+0

这非常简单,您可以使用Jquery的3行完成toggleAll()函数 – trichetriche

回答

0

如果你有一个组件item来处理所有事情,用你提供的代码,我会说你应该使用这个项目本身来处理它是否打开。

<ng-container *ngFor="let item of list"> 
    <item [content]="item.content" [title]="item.title" [open]="item.open" (click)="item.open = !item.open"></item> 
</ng-container> 

如果你需要切换开启/关闭的一切,你可以链接到一个函数像这样的按钮:

function toggleAll() { 
    this.collapseAll = !this.collapseAll 
    this.list.forEach(x => x.open = collapseAll) 
} 

如果您有更多相关的代码,可能会影响答案,编辑你问题,我会尽量调整答案。