var myapp = angular.module('sampleapp', [ ]); myapp.controller('samplecontoller', function ($scope) { }); // Common directive for Focus angular.module('sampleapp').directive('focus', function($timeout) { return { scope : { trigger : '@focus' }, link : function(scope, element) { scope.$watch('trigger', function(value) { if (value === "true") { $timeout(function() { element[0].focus(); }); } }); } }; } );
<div ng-app="sampleapp"> <div ng-controller="samplecontoller"> <div class="focusexample"> <h1>Auto Focus : </h1> With Out Focus : <input type="text"><br><br> Auto Focus : <input type="text" focus="true"><br><br> </div> </div> </div>