Rastographics
Rastographics

Reputation: 390

Ionic scrollTop "Cannot read property 'scrollTo' of null" (still exists in 1.0.0-rc.1)

This bug was referred to here: in ionic changing route causes "TypeError: Cannot read property 'scrollTo' of null"

The answer says that this bug was fixed in beta-13, but I'm using 1.0.0-rc.1 and the bug still appears.

In my case, it the error is showing when navigating back to a page that uses $ionicScrollDelegate.scrollTop()

Is anyone else getting this error after updating to rc.1?

EDIT: I find that if I do not call $ionicScrollDelegate.scrollTop() automatically when my view loads, the error does not come up. Should I be calling scrollTop() within a specific Ionic event that waits for the right time?

Upvotes: 6

Views: 10340

Answers (5)

Ashok Kumar
Ashok Kumar

Reputation: 1

Please change

e.detail.scrollTop

to

e.target.scrollTop

then this will Work

Upvotes: 0

garrettmac
garrettmac

Reputation: 8585

Im late to this but was getting the same error but by calling the scroll top element with:

$ionicScrollDelegate.scrollTop();

but rather:

var scrollTop = e.detail.scrollTop;

and fixed my by using the following:

var scrollTop = $ionicScrollDelegate.getScrollPosition().top;

Im also using js scrolling as it seems to work better with the scrolla-sista plugin so I have the following in my config block at the start of my app

  $ionicConfigProvider.scrolling.jsScrolling(true);

where their docs state:

Whether to use JS or Native scrolling. Defaults to native scrolling. Setting this to true has the same effect as setting each ion-content to have overflow-scroll='false'.

I hope this helps someone

Upvotes: 1

kev_panda
kev_panda

Reputation: 145

For what it's worth, I saw this solution on this thread here and it worked for me with version 1.0.0-beta.14

If upgrading to version 1.0.0-beta.14 is not an option, you can change the ionic-bundle.js file with the following:

Approximately Line 39910:

this.scrollTop = function(shouldAnimate) {
  this.resize().then(function() {
    if(typeof scrollView !== 'undefined' && scrollView !== null){
     scrollView.scrollTo(0, 0, !!shouldAnimate);
    }
  });
};

And Approximately line 39813:

if (!angular.isDefined(scrollViewOptions.bouncing)) {
  ionic.Platform.ready(function() {
    if(!scrollView){
      return;
    }
    scrollView.options.bouncing = true;

    if(ionic.Platform.isAndroid()) {
      // No bouncing by default on Android
      scrollView.options.bouncing = false;
      // Faster scroll decel
      scrollView.options.deceleration = 0.95;
    }
 });
}

Upvotes: 0

Amine Sa
Amine Sa

Reputation: 11

You can just put it in

$ionicPlatform.ready(function () {
    $ionicScrollDelegate.scrollTop();
})

Upvotes: 1

Rautec
Rautec

Reputation: 61

Had the same problem, even with v1.0.0 "uranium-unicorn".

Wrapping the scroll call into a $timeout helped - this is what it looks like in my code:

   $timeout(function() {
                $ionicScrollDelegate.scrollTo(scrollPosition.left, scrollPosition.top);
            }, 0);

Upvotes: 6

Related Questions