I am trying to add class to my sidebar when the viewport is less than or equal to 641px, and I have to watch the width of the window for that case
$scope.$watch(function(){
return $window.innerWidth;
}, function(value) {
if (value <= 641) {
logger.info('!!!less than 641');
vm.singleColumn = true;
};
});
It logs on first load, but when I resize, I have to do some clicks before it triggers again. E.g. I resized then I clicked on an item with ng-click behavior then that is the only time, it logs again.
I've read some of the questions, and it is likely due to $digest and $apply?
Can someone give a light to my confusion.
1) What user1162084 says
2) The approach with watch on function(){return $window.innerWidth;} will not work, because the resize of window do not cause the start of $digest cycle.
The $watch expression is reevaluated only in $digest cycle. No $digest cycle = no reevaluation. In angularjs $digest cycle is started:
a) After code in ng-click was executed
b) After function in $timeout or $interval was executed.
c) After http request made with $http was finished and success\error handler was executed
There may be and other cases, but the point is that resize of window do not belong to those types of events, which lead to start of $digest cycle.
And this also explains why with code you provided you get update only after ng-click execution