- /* Focus element when directive is linked */
- <input ui-focus-initial ng-model="user.username">
- angular.module('ui.mediture').directive('uiFocusInitial', function($timeout) {
- return {
- restrict: 'AC',
- link: function(_scope, _element) {
- $timeout(function(){ _element.focus(); }, 0);
- }
- };
- });
- /* focus element when scope expression is true */
- /* <div><input ui-focus-on="editingfoo" ng-model="foo"></div> */
- angular.module('ui.mediture').directive('uiFocusOn', function($timeout) {
- var directive = {
- restrict: 'AC',
- link: function(_scope, _element, attrs) {
- var watchVar = attrs[directive.name];
- var watchFunc = function () {
- if (_scope[ watchVar]) {
- $timeout(function () { _element.focus(); }, 1);
- }
- };
- _scope.$watch(watchVar, watchFunc);
- },
- self: this
- };
- return directive;
- });
- /** Wait for the next digest, then focus the first child element of the type */
- /* <div focusFirst="input"><div ng-include="search_template"></div></div> */
- app.directive("focusFirst", function($timeout) {
- return {
- link: function(scope, elem, attrs) {
- var selector = attrs.focusFirst || 'input';
- $timeout(function() {
- var found = elem.find(selector);
- if (found[0]) found[0].focus();
- });
- }
- }
- });
SHARE
TWEET
Untitled
a guest
Dec 18th, 2014
133
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

