I created a simple directive which triggers focus event on input to show tooltip
.directive('tooltipValidationObserver', ['$timeout', function($timeout) {
return {
restrict: 'A',
require: '?ngModel',
link: function($scope, element, attrs, ngModel) {
$scope.$watch(function() { return ngModel.$invalid; }, function(newVal, oldVal) {
$timeout(function() {
$('#' + attrs.id).trigger('focus');
});
});
}
}
}])
It works perfectly if I use attrs.id, but if I want to perform
$(element).trigger('focus')
element is undefined, when in link function it's defined on linking phase.. and undefined in watch function. Why? In watch function I can obtain all available variables (such as $scope, attrs, ngModel) but not the element..