我有一个 productsItems 数组,它是通过Input()来自父元素的,我希望在每个元素被移除时添加动画。
但是,动画正在运行所有元素。是否可以仅在删除的项目上运行动画。
animation.ts
import { trigger, state, animate, transition, style, sequence } from
'@angular/animations';
export const SlideToggleAnimation =
trigger('slideState', [
state('show', style({
opacity: 1,
visibility: 'visible'
})),
state('hide', style({
opacity: 0,
visibility: 'hidden'
})),
transition('show => hide', animate('300ms ease-out')),
transition('hide => show', animate('300ms ease-in')),
transition('* => void', [
style({ height: '*', opacity: '1', transform: 'translateX(0)', 'box-shadow': '0 1px 4px 0 rgba(0, 0, 0, 0.3)' }),
sequence([
animate(".25s ease", style({ height: '*', opacity: '.2', transform: 'translateX(20px)', 'box-shadow': 'none' })),
animate(".1s ease", style({ height: '0', opacity: 0, transform: 'translateX(20px)', 'box-shadow': 'none' }))
])
]),
transition('void => *', [
style({ opacity: '0', transform: 'translateX(20px)' }),
sequence([
animate(".2s ease", style({ opacity: '1', transform: 'translateX(2px)' })),
])
])
])
我通过output()检测删除按钮操作,并使用它来确定动画的状态。 这是因为 productItems ,因为它在每次更改列表后都会更新。
main.component.ts
<li class="shopping-cart__item" *ngFor="let product of productsItems"
[@slideState]="product.isRemoved ? 'hide' : 'show'"
(@slideState.start)="animStart($event)"
(@slideState.done)="animEnd($event)">
....
<app-remove-button [productId]="product._id" (isRemoved)="onRemove($event)" appDeleteIconHover></app-remove-button>
</li>
答案 0 :(得分:1)
我能够通过trackBy来做到这一点。
<强> main.component.ts 强>
trackByProductId(index: number, product: any): string {
return product._id;
}
<强> main.component.html 强>
<li class="shopping-cart__item"
*ngFor="let product of productsItems;
trackBy:trackByProductId" [@slideState]="product">
...
...
</li>