2017-06-15 125 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) 
} 

如果您有更多相關的代碼,可能會影響答案,編輯你問題,我會盡量調整答案。