我正在嘗試爲投資組合構建一些有意義的議案。目標是當用戶點擊工作項目時,該項目彈出併成長。要做到這一點,我相信我必須改變數組內部點擊元素的狀態,但是現在當我點擊它時,它會影響數組內部所有元素的所有狀態,這些狀態都是一起動畫的。任何人都可以解釋我發生了什麼以及如何解決?Angular 2/4爲數組內的一個項目添加動畫而不是所有項目
portfolio.component.ts
import { Component } from '@angular/core';
import { trigger,style,transition,animate,keyframes,state,query,stagger } from '@angular/animations';
export class Job {
id: number;
title: string;
}
const JOBS: Job[] = [
{ id: 11, title: 'Item 1' },
{ id: 12, title: 'Item 2' },
{ id: 13, title: 'Item 3' },
];
@Component({
selector: 'portfolio',
templateUrl: './portfolio.component.html',
styleUrls: ['./portfolio.component.css'],
animations: [
trigger('enlarge', [
state('small', style({
height: '100px',
})),
state('large', style({
height: '200px',
background: 'red',
})),
transition('small <=> large',
animate('1s ease-in')),
]),
]
})
export class PortfolioComponent {
title = 'Portfolio';
jobs = JOBS;
state: string = 'small'
enlarge() {
this.state = (this.state === 'small' ? 'large' : 'small');
}
}
portfolio.component.html
<div class="list"
*ngFor='let job of jobs'
[@enlarge]='state'
(click)="enlarge()">
<div for="#">{{job.id}} - {{job.title}}</div>
</div>
感謝您快速的答案,但我應該在哪裏把步驟1中的代碼?我試圖把它放在我的課堂上,但我收到一個錯誤:意外的令牌。期望構造函數,方法,訪問器或屬性。 –
嘗試更新的代碼@OsmarMatos。把它放在你得到你的工作清單之後.. – TheUnreal
嘗試過,但現在錯誤屬性'狀態'不存在類型'作業'在屏幕上和Uncaught TypeError:無法讀取控制檯上未定義的屬性'地圖' - @ TheUnreal –