I have a component 'bar' inside ngFor. I want to update its width with animation starting from the previous value to new one.
html :
<div *ngFor="let station of stations" class="station">
<bar [perc]="station.perc" class="bar"></bar>
</div>
ParentComponent :
ngOnInit(){
setInterval(() => this.updateData(), 10000);
}
updateData(){
const url = 'http://....';
this.http.get(url)
.map(res => res.json())
.subscribe(data => {
this.stations = data;
});
}
BarComponent
export class BarComponent {
@Input() perc;
constructor(private element: ElementRef){}
ngOnChanges(changes: any): void {
let perc = changes.perc.currentValue;
TweenMax.to(this.element.nativeElement, 1, { width: perc + '%' });
}
}
But on each updateData() it looks like ngFor recreates the dom and BarComponent is constructed again. So even if 'station.perc' is the same, ngOnChanges() is fired.
And the transition keeps on restarting from 0 instead of starting from the previous value...
I probably miss a crucial point here but whats the clean workaround?