0

I have here a list of div, I want to slide a particular element on click. Currently all elements are sliding as state is a single variable for all elements.

.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';
  }
}
Sai Datta
  • 895
  • 1
  • 9
  • 25

1 Answers1

0

Okay so I implemented a Hacky solution:

<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>

let me run you through what I did.

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

Firstly, that condition checks if I clicked the delete button or not.

If I did, then it's state will be evaluated to active , in which case the animation plays from inactive to active state , thus moving it to the left.

And also, when I click the delete button the ClickedDelete(j) Function is called

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

And then on completition of animation the DeleteThisItem() function is called with this callback (@slideOutAnimation.done)="DeleteThisItem($event)"

Then i'm splicing the item from the array in the DeleteThisItem() function.

Sai Datta
  • 895
  • 1
  • 9
  • 25