0

I want to do a progress bar when user scrolls down. When it's down all the way, the bar is 100% width.

The problem is the "per" value is not updated to view on scroll event

I have checked this and this, but none worked

Template

<ion-header>
  <ion-navbar color="primary">
    <ion-title>{‌{title}} ({‌{code}})</ion-title>
  </ion-navbar>
</ion-header

<ion-content>
<ion-toolbar style="position: fixed; opacity: 0.8; z-index: 1" *ngIf="readProgress">
    <div id="bar" color="primary">{‌{per}} 30%</div>
</ion-toolbar>
</ion-content>

TS FILE

this.content.ionScroll.subscribe((data) => {
  if (data.scrollTop < 1) {
    this.readProgress = false;
    this.title='Not Scrolling';
  } else if (data.scrollTop > 0) {
    this.readProgress = true;
    this.title='Scrolling';
  }
  this.per = data.scrollTop;  
});
Community
  • 1
  • 1

1 Answers1

0

I finally got it working by making Angular detect changes after the 'per' value changes

1. Imports

import {ChangeDetectorRef, Component, EventEmitter, Output, ViewChild} from "@angular/core";

2. Constructor

constructor(public navCtrl: NavController, public navParams: NavParams, public cdRef: ChangeDetectorRef) {
      }

3. On Scroll Event

   this.content.ionScroll.subscribe((data) => {
          if (data.scrollTop < 1) {
            this.readProgress = false;
            this.title='Not Scrolling';
          } else if (data.scrollTop > 0) {
            this.readProgress = true;
            this.title='Scrolling';
          }
          this.per = data.scrollTop;  
            this.cdRef.detectChanges();
        });