How do I get animations to fire on removal of a dom element in Angular2?
import { Component, Input, Output, EventEmitter } from '@angular/core';
import { FormGroup, FormControl, Validators } from "@angular/forms";
import {
trigger,
state,
style,
animate,
transition
} from '@angular/animations';
import { ServerService } from './server.service';
@Component({
selector: 'fc-server',
templateUrl: './server.component.html',
animations: [
trigger('flyInFlyOut', [
state('*', style({transform: 'translateX(0)', opacity: 1})),
transition('void => *', [
style({transform: 'translateX(-100%)', opacity: 0}),
animate('400ms ease')
]),
transition('* => void', [
style({transform: 'translateX(-100%)'}),
animate('400ms ease')
])
])
]
})
export class Server {
@Input() serverInstance;
serverForm;
constructor(
private serverService: ServerService) {}
ngOnInit() {
this.serverForm = new FormGroup({
serverName: new FormControl('', Validators.required),
serverPort: new FormControl('', Validators.required),
serverIp: new FormControl('', Validators.required),
});
}
@Output() remove = new EventEmitter();
onRemove() {
this.serverService.remove(this.serverInstance);
}
onSubmit(serverInstance) {
this.serverService.add(serverInstance);
}
}
Everything works, removal of the item from the list, adding items to the list, animation in...except when an item is removed, the list item is deleted with no animation. Any insight?
It seems like a reoccurring issue with Angular2, however nobody really has a good resolution as far as I've seen.
For instance, this article: https://www.bennadel.com/blog/3140-using-changedetection-with-animation-to-setup-dynamic-void-transitions-in-angular-2-rc-6.htm
You need to define the end
stateof yourvoidtoo:state('void', style({transform: 'translateX(100%)', opacity: 0}))Then removes the
style({transform: 'translateX(-100%)'}),line in your* => voidtransition.Or I would write it in , in my opinion, an easier form to understand:
The
:leaveand:enterare shorthand for* => voidandvoid => *, respectively.