0

I try to detect when the user scroll the view and then I would like to switch view.

I'm using a directive for detect when the user scroll :

.directive("scroll", function ($window) {
return function(scope, element, attrs) {
    angular.element($window).bind("scroll", function() {
        if (this.pageYOffset >= 1) {
            scope.boolChangeClass = true;
            console.log('Scrolled down');
        } else {
            scope.boolChangeClass = false;
            console.log('Scroll up');
        }
        scope.$apply();
    });
};

The directive is called on the div with the ng-controller

<div scroll ng-controller="MyCtrl1">

I get the boolChangeClass in the view of Controller 1

   <span>{{boolChangeClass}}</span>

And log the result in my controller

myApp.controller('MyCtrl1', ['$scope', '$http', function($scope, http) {
   console.log($scope.boolChangeClass);
}]);

How can I detect in my controller that boolChangeClass have changed and then I would like to go to controller2 ?

2 Answers 2

1

Use $watch to detect any change in the scope variable.

myApp.controller('MyCtrl1', ['$scope', '$http', function($scope, http) {
   console.log($scope.boolChangeClass);

   $scope.$watch(function(){
       return $scope.boolChangeClass;
   }. function() {
      console.log($scope.boolChangeClass); // will be called when the values gets changed
   })

}]);
Sign up to request clarification or add additional context in comments.

Comments

0

This is where you should use services to share data among different controllers..

Or if you really want to do in simple, just define a baseController in upper root div and define your boolChaneClass in that baseController so its accessible from both child controllers (ctrl1, ctrl2 ...)

Comments

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.