I currently use
scope.onFocus = function(){ // change scope variables
};
elem.bind('focus', function(){
scope.$apply('onFocus()')});
to bind to the focus event in the link function of a directive.
The problem is if I fire the focus event manually like this: elem.focus() in say ng-click handler , I will get "apply is in progress" error.
Is the workaround to check whether apply has been called : if (! scope.$$phase) ?
Is it considered "Angularic" to check before calling apply?
Any other elegant solution?
UPDATED:
Here's my solution:
Since scope.onFocus can trigger external events (outside of Angular) which in turn can call $apply, you would have 'apply already in progress' error. The trick is to call $apply separately.
element.bind('focus', function(){
scope.onFocus();
scope.$apply(); // don't wrap onFocus call in $apply
})
elem.focus()in ng-click? Why notonFocus()? - joakimbl