1

I want to change value of a variable when the value of scroll position is greater than 100 from top but it doesn't work, the value doesn't change in $scope. Here is the code:

 <div ng-show="title===true">
  <p>{{title}}</p>
  <p>{{card.nome}}</p>
  <p>{{card.prezzo}}€</p>
</div>

<ion-content style="top:0px" delegate-handle="cardScroll" on-scroll="getPositionScroll()">

$scope.title = true;
  $scope.getPositionScroll = function () {
    console.log("scrollPosition " + JSON.stringify($ionicScrollDelegate.$getByHandle('cardScroll').getScrollPosition().top));
    console.log("valore title " + $scope.title);
    console.log($ionicScrollDelegate.$getByHandle('cardScroll').getScrollPosition().top >= 100);
    $scope.title = $ionicScrollDelegate.$getByHandle('cardScroll').getScrollPosition().top >= 100;
  };

Does anyone know why this is not working?

Dexter
  • 2,462
  • 4
  • 24
  • 28
Claudiu
  • 47
  • 8

1 Answers1

0

I think it may have to do with the fact that Angular doesn't track changes that have been made by the getPositionScroll() function. A lot of Angular functions are wrapped in the $scope.$apply() function which notifies Angular about changes that have been made. But I think that the function used to track scrolling is not wrapped in this function, which means that you need to call it yourself.

So after $scope.title add:

$scope.$apply($scope.title);

There are a lot of questions regarding when to use $scope.$apply(), like this one, if overused it can cause serious harm to the performance of your app. There is also information about this in the Angular documentation. You may also find some insight in this question about when to use this function in a scroll event.

Dexter
  • 2,462
  • 4
  • 24
  • 28