I am using the $broadcast pattern outlined here to controllers to listen to their service's state.
for example, here I listen to the service's busy/unbusy state to set the mouse cursor.
//Our service
function startRunning(){
$rootScope.$broadcast("busy");
}
function stopRunning(){
$rootScope.$broadcast("unbusy");
}
//Our controller
$scope.$on("busy", function(){
$scope.state = "busy";
});
$scope.$on("unbusy", function(){
$scope.state = "ready"
});
HTML
<div ng-controller = "myctrl" ng-state = "state"/>
CSS:
.busy{
cursor: wait;
}
.ready {
cursor:auto;
}
The problem with this is that the cursor doesn't change immediately. It usually requires me to move the mouse, I imagine to trigger the $digest
cycle, before the cursor changes.
I can fix that with
$scope.$apply($scope.state = "ready");
But this will throw up:
Error: [$rootScope:inprog] $digest already in progress
errors.
What is the best way to deal with this?
Edit: here's a working JSFiddle: http://jsfiddle.net/HB7LU/23512/
The issue appears to be something to do with using non-angular timeout/asynchronous methods. (ie. issue doesn't appear if using a $timeout
, but does appear if using a setTimeout
;