AngularJS 1.2.0rc1 Focused Directive
Angular directive for monitoring when an element has focus.
by GlobalDomestic
HTML
<script src="http://code.angularjs.org/1.2.0rc1/angular.js"></script>
<div ng-controller="MyCtrl">
<input type="text" placeholder="Focus me!" myng-focused="focused"/>
<pre ng-show="focused">I'm focused!!!</pre>
</div>
JavaScript
var myApp = angular.module( 'myApp', [] );
myApp.controller( 'MyCtrl', [ '$scope', function ( $scope ) {}]);
myApp.directive( 'myngFocused', [ '$compile', function ( $compile ) {
return function ( scope, element, attr ) {
var scopeVar = attr.myngFocused;
attr.$set( 'myngFocused' );
attr.$set( 'ngFocus', scopeVar + ' = true' );
attr.$set( 'ngBlur', scopeVar + ' = false' );
$compile( element )( scope );
};
}]);