Amit Agarwal
Amit Agarwal

Reputation: 61

when scrolled $(window) scroll function not triggered at angular controller

I want to fix some new panel when window is scrolled but, whenever I scrolled window then $(window).scroll(fucntion(){ }); is not triggered. This function is in my angular controller.

This is at angular controller but when I scroll it isn't triggered

$(window).scroll(function () {
            console.log(" i Like it");
        //$scope.$apply();
    });

I also tried this.

$(document).ready(function(){ 
$(window).scroll(function () {
            console.log(" i Like it");
        //$scope.$apply();
    });
});

But it's also not working. Please help.

Upvotes: 1

Views: 349

Answers (2)

thegio
thegio

Reputation: 1243

May I ask why are you using parenthesis? ($(window))

Please try following code:

app = angular.module('myApp', []);
app.controller("ctrl", function ($window) {
  var i = 0;

    $window.onscroll = function(){
     console.log(" i Like it: " + i++);
   };
});

Should be triggered by window scroll. But honestly you should really consider the use of directives instead.

Upvotes: 1

Jordi Ruiz
Jordi Ruiz

Reputation: 487

when you work with angular you have to think about separation of concerns. You shouldn't add this kind of functionality inside of an angular controller. If you want to do something with the DOM probably you should use an angular directive.

Regards

Upvotes: 0

Related Questions