Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
  <title>Ng-ninja: AngularJS : Focus Me directive</title>
  <link href="https://fonts.googleapis.com/css?family=Roboto|Roboto+Condensed" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
</head>
  
<body ng-app="MyApp" ng-controller="myController">

  <a href="http://ngninja.com/" target="_blank">
    <h1 class="blog">Ng-ninja</h1>
  </a>
  
  <h4>AngularJS : Focus Me directive</h4><br>
  
  <h4 class="header">1. Trigger focus by Button click event</h4>
  <button class="btn btn-primary" ng-click="focusTriggerInput=true">Trigger</button>
  <div class="form-group">
    <label class="caption" for="triggerInput">Trigger Input</label>
    <input type="text" id="triggerInput" name="triggerInput"
           focus-me="focusTriggerInput"
           ng-model="triggerInput" />
  </div>
  <br><br>
  
  <h4 class="header">2. Trigger focus when parent container is made visible</h4>
  <button class="btn btn-primary" ng-if="isParentVisible" ng-click="toggleParent()">Hide Parent</button>
  <button class="btn btn-primary" ng-if="!isParentVisible" ng-click="toggleParent()">Show Parent</button>
  <div class="form-group parent" ng-if="isParentVisible">
    <label class="caption" for="visibleInput">Parent Visible Input</label>
    <input type="text" id="visibleInput" name="visibleInput"
           focus-container=".parent" focus-me="focusVisibleInput"
           ng-model="visibleInput" />
  </div>
  
</body>
</html>
              
            
!

CSS

              
                body{
  width: 100%;
  height: 100%;
  background-color: #4b4b4b;
  font-family: Roboto;
  color: white;
  padding: 16px;
}

a{
  color: blue;
}

h1.blog{
  color: yellow;
}

input {
  height: 30px;
  font-size: 21px;
  padding-left: 10px;
  color: black;
}

h4{
  font-weight: normal;
}

h4.header {
  color: grey;
}

.form-group label {
  margin-right: 8px;
}


              
            
!

JS

              
                angular.module('MyApp', [])
  .directive('focusMe', function($timeout) {
        return {
            scope: { 
                trigger: '=focusMe',
                focusContainer: '@'
            },
            link: function(scope, element) {
                // Focus if focusme is triggered
                scope.$watch('trigger', function(value) {
                    if(value === true) { 
                        $timeout(function() {
                            element[0].focus();
                            scope.trigger = false;
                        });
                    }
                });

                // Focus if parent container is made visible
                scope.$watch(function() { return element.attr('class') }, function(newValue) {
                    $timeout(function() {
                      if (newValue.match(/ng-hide/) !== null) {
                        return; // Element is hidden.
                      } 
                      
                      element[0].focus();
                      scope.trigger = false;
                    });
                });
            }
        };
    })

 .controller('myController', ['$scope', myController]);

function myController($scope){
  $scope.isParentVisible = false;  
  
  $scope.toggleParent = function(){
    $scope.isParentVisible = !$scope.isParentVisible;
  };
}

              
            
!
999px

Console