Given the following directive
directive('myDirective', function() {
return {
restrict: 'A',
scope: {},
replace: false,
template: '<input ng-focus="onFocus()" type="text" />',
link: function(scope, element, attr) {
scope.onFocus = function() {
console.log('got focus');
};
}
};
});
I've tested that the focus watcher works in a browser, but I'd like to be able to trigger it in a unit test. This is what I've tried but it isn't working.
var element = angular.element('<div my-directive></div>');
$compile(element)($scope);
$scope.$digest();
element.find('input')[0].focus();
I can see that I am correctly getting to the input box with the find call, and I would expect that code to trigger the focus event on the input box, but nothing is happening. What am I missing here?
element.find('input')
the element is the input tag – Irredeemableelement
is the div and contained within it is the input tag. – Scarfaceelement.find('input').triggerHandler('focus')
; – Irredeemable$(element.find('input')[0]).triggerHandler('focus');
– Scarface