I have a directive that adds 'fixed' class by:
ng-class='{ "fixed": fixed }'
The directive has the following function:
angular.element($window)
.on('scroll', function() {
if (this.pageYOffset >= 480) {
if (!scope.fixed) {
scope.fixed = true;
scope.$apply();
}
} else {
if (scope.fixed) {
scope.fixed = false;
scope.$apply();
}
}
});
My question is, should I apply the scope.$apply()
like this, so it only fires whenever my variable changes, or is it ok just to write:
angular.element($window)
.on('scroll', function() {
if (this.pageYOffset >= 480) {
scope.fixed = true;
} else {
scope.fixed = false;
}
scope.$apply();
});
Maybe there's some use of scope.$watch
that I am unaware of?