I have a watch that triggers a DOM event:
scope.$watch(function() { return controller.selected; }, function(selected) {
if (selected) {
$input.trigger('focus');
}
});
The issue is that I have a handler on 'focus' that does a scope.$apply
.
$input.bind('focus', function() {
scope.$apply(function() { controller.focused = true; });
});
So when my $watch
is fired from inside a $digest
it causes an error because it tries to trigger another $digest
.
The workaround I have is to put the trigger in a $timeout
.
scope.$watch(function() { return controller.selected; }, function(selected) {
if (selected) {
$timeout(function() { $input.trigger('focus'); });
}
});
This works ... so far. Is this the proper way to handle this? I'm not sure if this catches every case and would like to see if there is an angular approved way to have a piece of code defer for after the digest.
Thanks!