0

我这里有一个列表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';
  }
}
4

1 回答 1

0

好的,所以我实现了一个 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,在这种情况下,动画会从inactiveactive状态 ,从而将其向左移动。

而且,当我单击delete按钮时,ClickedDelete(j)函数被调用

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

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

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

于 2017-07-18T09:42:51.520 回答