-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-auto-focus.js
More file actions
35 lines (30 loc) · 867 Bytes
/
angular-auto-focus.js
File metadata and controls
35 lines (30 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(function(){
'use strict';
angular.module('angular-autofocus', []);
angular
.module('angular-autofocus')
.directive('autofocus', ['$timeout', '$parse', directiveImpl]);
function directiveImpl($timeout, $parse) {
return {
restrict: 'A',
link: linkImpl
};
function linkImpl(scope, elem, attrs) {
var input = elem[0].nodeName === 'INPUT' ? elem : elem.find('input');
var isFocused = false;
attrs.$observe('autofocus', function(newVal, oldVal){
var shouldAutofocus = scope.$eval(attrs.autofocus);
if(shouldAutofocus) {
$timeout(function(){
input[0].focus();
});
} else if(!shouldAutofocus && isFocused) {
$timeout(function(){
input[0].blur();
});
}
isFocused = shouldAutofocus;
});
}
}
})();