Skip to content

Instantly share code, notes, and snippets.

@lovelykaushik86
Created March 4, 2014 10:05
Show Gist options
  • Save lovelykaushik86/9343612 to your computer and use it in GitHub Desktop.
Save lovelykaushik86/9343612 to your computer and use it in GitHub Desktop.
AngularJS ngFocusOut directive - If you are new in angularJs you can use this by adding in your HTML tag. e.g. <input type = "text" ng-focus-out = "$scope.bool = true">
appStrapDirectives.directive('ngFocusOut', function( $timeout ) {
return function( $scope, elem, attrs ) {
$scope.$watch(attrs.ngFocusOut, function( newval ) {
if ( newval ) {
$timeout(function() {
elem[0].focusout();
}, 0, false);
}
});
};
});
@jusopi
Copy link

jusopi commented Oct 22, 2014

I too needed something like this... wrote my own directive, worked like a charm. Then I discovered that this is essentially the ng-blur directive. https://docs.angularjs.org/api/ng/directive/ngBlur

@crediblebytes
Copy link

Except when you need to do something before the element loses focus and not after the fact. Very useful if you are trying to save out selection from text area/input before losing focus for drag and drop.

@passatgt
Copy link

passatgt commented Aug 29, 2018

you could use the built-in ng-blur instead:

ng-focus="focused=true" ng-blur="focused=false"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment