Created
July 21, 2015 01:47
-
-
Save ChongTang/55fb59a02c1ff2e5b9aa to your computer and use it in GitHub Desktop.
AngularJS auto focus on another input when an input's text reach certain length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The AngularJS code | |
var app = angular.module('autofocus', []); | |
app.directive('autofocusWhen', function () { | |
return function (scope, element, attrs) { | |
scope.$watch('maxLengthReach', function(newValue){ | |
if (newValue.length >= 5 ) { | |
element[0].focus(); | |
} | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The HTML code | |
<div ng-app="autofocus"> | |
<label>Name:</label> | |
<input ng-model="maxLengthReach"></input> | |
<br/><br/> | |
<label>Title:</label> | |
<input autofocus-when></input> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment