Angular $ watch window.pageYOffset not updating?

In my controller, I have:

$scope.woffset = window.pageYOffset;

 $scope.$watch("woffset", function (newValue, oldValue) {
    console.log("hello");
    console.log(window.pageYOffset);
 }, true);
});

So, when I scroll, I should get the hello console logs when changing pageYOffset. However, he does nothing. But if I run window.pageYOffset in the console while scrolling down, I see that the value is changing. Any ideas?

I tried several variations of the clock (with and without true, using functions instead of strings, but nothing works).

(I know there is work with onscroll, but I would like to know how it will work like that) Thanks!

Edit: this doesn't work either:

$scope.test = function () { 
    return window.pageYOffset;
}

$scope.$watch("test", function (newValue, oldValue) {
console.log("hello");
console.log(window.pageYOffset);
}, true);
+4
source share
3 answers

, $scope.woffset - . :

var i = 5;
var j = i;

i = 7;
// Wondering here why j isn't 7

- .

-:

window.onscroll = function () {
    console.log("hello");
    console.log(window.pageYOffset);
    // any $scope variable updates
    $scope.$digest();
};

, , . "debouncing" - , , <input> AngularJS _lodash?

, , . , :

$scope.$on('$destroy', function() {
    window.onscroll = null;
});
+1

, window.pageYOffset . , window.pageYOffset , . , , , .

0
angular.element($window).on("scroll resize", function (e) {
console.log($window.pageYOffset)
});

scope.$on('$destroy', function () {
angular.element($window).unbind("scroll resize");
});
0
source

All Articles