1

Is there a way to detect, if a element was scrolled and call a function and do some stuff with angularJS? Something like a ng-scroll or so, which works simply like a ng-click? I didn't really find a solution...

This is what I would like to do:

angular.module("myApp", []).controller("myController", function($scope) {
  $scope.checkIfScrolling = function() {
    console.log("scrolled...");
  }
});
.box-scroll {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100px;
  background-color: lightgrey;
  overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div app="myApp" controller="myController" class="box-scroll" ng-scroll="checkIfScrolling()">
  <div class="box-item">I'm an item within box-scroll</div>
  <div class="box-item">I'm an item within box-scroll</div>
  <div class="box-item">I'm an item within box-scroll</div>
  <div class="box-item">I'm an item within box-scroll</div>
  <div class="box-item">I'm an item within box-scroll</div>
  <div class="box-item">I'm an item within box-scroll</div>
  <div class="box-item">I'm an item within box-scroll</div>
  <div class="box-item">I'm an item within box-scroll</div>
  <div class="box-item">I'm an item within box-scroll</div>
  <div class="box-item">I'm an item within box-scroll</div>
  <div class="box-item">I'm an item within box-scroll</div>
</div>

2 Answers 2

1

Nothing native, you'd have to wire up your own directive using the onscroll event:

https://developer.mozilla.org/en-US/docs/Web/Events/scroll

Sign up to request clarification or add additional context in comments.

Comments

1

It can be achieved without any directive also

window.addEventListener('scroll', function(e) {
  //action to be performed
});

2 Comments

What's the best way to put this in a controller of a ng component?
Create a directive as ng-scroll and then u can add this functionality inside the link: of the diretive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.