Angular 2 - 如何为特定的 div 实现路由器动画?

Angular 2 - How to implement Router Animation for a particular div?

我这里有一个 div 列表,我想在单击时滑动特定元素。目前所有元素都在滑动,因为状态是所有元素的单个变量。

.html

<div *ngFor="let item of [1,2,3,4,5]" [@slideOutAnimation]="state" (@slideOutAnimation.done)="onDone($event)">
   <button (click)="DeleteItem(item)">Delete</button>
</div>

.ts

@Component({
  selector: 'page-box',
  templateUrl: 'box.html',
  animations:[
      trigger('slideOutAnimation', [
          state('inactive',style({
              transform: 'translateX(0%)'
          })),
          state('active',style({
            transform: 'translateX(-200%)'
          })),
          transition('inactive => active', animate('500ms ease-out'))
        ])
    ]
})

export class BoxPage{

 state:string = 'inactive';

 DeleteItem(item){
    this.state = 'active';
  }
}

好的,所以我实施了一个 Hacky 解决方案:

<div class="card-w" (@slideOutAnimation.done)="DeleteThisItem($event)" [@slideOutAnimation]=" (selecteditem == j) ? 'active':'inactive' " *ngFor="let item of list; let j = index;">
 <button (click)="ClickedDelete(j)">Delete</button>
</div>

让我 运行 你通过我所做的。

[@slideOutAnimation]=" (selecteditem == j) ? 'active':'inactive'

首先,该条件检查我是否单击了 delete 按钮。

如果我这样做了,那么它的状态将被评估为 active ,在这种情况下,动画从 inactive 播放到 active 状态,从而将其向左移动。

此外,当我单击 delete 按钮时,会调用 ClickedDelete(j) 函数

ClickedDelete(index){
  this.selecteditem = index;
}

然后在动画完成时使用此回调调用 DeleteThisItem() 函数 (@slideOutAnimation.done)="DeleteThisItem($event)"

然后我在 DeleteThisItem() 函数中拼接数组中的项目。