Last active
August 29, 2015 14:27
-
-
Save jenslohmann/4b852942f02c5b8d9ea4 to your computer and use it in GitHub Desktop.
How to move focus to a field the angular way
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
... | |
<textarea focus-on="IWantFocusWhenButtonClicked"></textarea> | |
<button ng-click="doFocus()">Focus</button> | |
... |
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
... | |
$scope.doFocus = function() { | |
$scope.$broadcast("focus:IWantFocusWhenButtonClicked"); | |
} |
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
angular.directive('focusOn', ['$timeout', ($timeout) => { | |
return { | |
restrict: 'A', | |
link: (scope, element, attr) => { | |
scope.$on("focus:" + attr.focusOn, () => { | |
$timeout(element[0].focus()); | |
}); | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment