I am trying to update a service variable on resize. The update doesn't seem to be registered by the controller, even though I'm using angular.element to bind the event.
The initial setDevice() works, but on resize when I log the Device service on window resize from the controller there is no change to the service. I tried $rootScope.$apply to broadcast changes but this doesn't work either.
Service:
angular.module('health.device', [])
.service('Device', function($window, $rootScope){
var Device;
function setDevice(dontApplyScope){
Device = {
isMobile: Modernizr.mq('only screen and (max-width: 40em)'),
isTablet: Modernizr.mq('only screen and (min-width: 40.063em) and (max-width: 64em)')
};
if(!dontApplyScope){
return $rootScope.$apply();
}
}
setDevice(true);
angular.element($window).bind('resize', setDevice);
return Device;
})
In the controller:
$scope.device = Device;
angular.element($window).bind('resize', function(){
console.log(Device); // doesn't change
})