2017-05-20 32 views
1

我試圖動畫點擊不只是動畫其餘如何觸發動畫特定項目,而不是所有的項目列表中的

@Component({ 
    selector: \\\\\ 
    templateUrl: \\\\ 

animations: [ 
trigger('showFull', [ 
    state('shrink', style({ 
    height: '50px' 
    })), 
    state('expand', style ({ 
    height: '*' 
    })), 
    transition('shrink <=> expand', animate('300ms ease-in')) 
]) 
] 
}) 
/// 

export class myComp { 
    state: string = 'shrink'; 

} 

toggleExpansion(){ 
    this.state = (this.state === 'shrink' ? 'expand' : 'shrink'); 
} 

的.html:

<div *ngFor="let item of items"> 
    <ion-item [@showFull]="state" (click)="toggleExpansion()"> 
    {{item.description}} 
    </ion-item> 
</div> 

如何/什麼傳遞給函數,以便state不會更改所有列出的項目。

在一個側面的問題,如何提高動畫代碼摺疊和展開

enter image description here

上面是當狀態爲expand

enter image description here

,這時候狀態shrink它可以注意到,該段的開始也崩潰/覆蓋...如何動畫擴展和崩潰從&到一個特定的點!

+0

你需要有狀態列表中的每個項目。 –

+0

這是什麼理想的解決方案,因爲我的項目列表是從服務器端和長度將是未知的,也用戶將添加和刪除這些項目..我這樣做截斷和顯示段落 – Yasir

+0

你可以添加一個屬性'狀態',當你從服務器端獲得對象時。 –

回答

1

從服務器端獲取對象時,可以在項目中添加屬性狀態。

this.items.forEach(item => item.state = "shrink"); 

在你的HTML:

<div *ngFor="let item of items"> 
    <ion-item [@showFull]="item.state" (click)="toggleExpansion(item)"> 
    {{item.description}} 
    </ion-item> 
</div> 

最後的組件,

toggleExpansion(item:any){ 
    item.state = (item.state === 'shrink' ? 'expand' : 'shrink'); 
} 
+0

我的項目是對象的列表,這就是爲什麼我想我在'forEach'接收錯誤返回undefined – Yasir

+0

我給了foreach作爲一個例子..它取決於你在哪裏設置陣列..只需設置此屬性以及 –

+0

thanx兄弟,關於動畫本身的任何建議 – Yasir

相關問題