SHARE
TWEET

Untitled

a guest Dec 18th, 2014 133 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Focus element when directive is linked */
  2. <input ui-focus-initial ng-model="user.username">
  3. angular.module('ui.mediture').directive('uiFocusInitial', function($timeout) {
  4.     return {
  5.         restrict: 'AC',
  6.         link: function(_scope, _element) {
  7.             $timeout(function(){ _element.focus(); }, 0);
  8.         }
  9.     };
  10. });
  11.  
  12.  
  13. /* focus element when scope expression is true */
  14. /* <div><input ui-focus-on="editingfoo" ng-model="foo"></div> */
  15. angular.module('ui.mediture').directive('uiFocusOn', function($timeout) {
  16.     var directive = {
  17.         restrict: 'AC',
  18.         link: function(_scope, _element, attrs) {
  19.             var watchVar = attrs[directive.name];
  20.             var watchFunc = function () {
  21.                 if (_scope[ watchVar]) {
  22.                     $timeout(function () { _element.focus(); }, 1);
  23.                 }
  24.             };
  25.             _scope.$watch(watchVar, watchFunc);
  26.         },
  27.         self: this
  28.     };
  29.     return directive;
  30. });
  31.  
  32.  
  33.  /** Wait for the next digest, then focus the first child element of the type */
  34. /* <div focusFirst="input"><div ng-include="search_template"></div></div> */
  35.   app.directive("focusFirst", function($timeout) {
  36.     return {
  37.       link: function(scope, elem, attrs) {
  38.         var selector = attrs.focusFirst || 'input';
  39.         $timeout(function() {
  40.           var found = elem.find(selector);
  41.           if (found[0]) found[0].focus();
  42.         });
  43.       }
  44.     }
  45.   });
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. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top