Check out this Demo. Utilizing ViewChild
is a great way to achieve this.
When working in HTML, it's important to make the value parametric.
<h1 #counter [countUp]="param">0</h1>
Within the component:
@ViewChild('counter') counter: ElementRef;
param=0;
By using HostListener, we can listen for scroll events and update the count when necessary (e.g., changing the count to 365 when the scroll reaches a certain point).
@HostListener('window:scroll', ['$event']) // for window scroll events
onWindowScroll(event) {
var rect = this.counter.nativeElement.getBoundingClientRect();
var elemTop = rect.top;
var elemBottom = rect.bottom;
(elemTop >= 0) && (elemBottom <= window.innerHeight) ? this.param=365:null
}