all files / app/components/autoFocus/ autoFocus.service.js

100% Statements 9/9
100% Branches 0/0
100% Functions 5/5
100% Lines 9/9
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 36 37 38 39 40 41 42 43                          178×                                          
/**
 * @ngdoc service
 * @name app.components.autoFocus
 * @description
 * Use this service with the feAutoFocus directive to set focus to an element.
 **/
 
(function () {
  'use strict';
 
  angular
    .module('app.components')
    .factory('autoFocus', factory);
 
  /** @ngInject */
  function factory($timeout, $rootScope) {
    return focus;
 
    /**
     * @ngdoc function
     * @name focus
     * @methodOf app.components.autoFocus
     * @description
     * Set the focus to an element containing an fe-auto-focus attribute.
     *
     * @param {Object} scope
     * Controller scope
     * @param {String} name
     * Value of a fe-auto-focus attribute.
     */
    function focus(scope, name) {
      var timer = $timeout(function () {
        $rootScope.$broadcast('fe-autoFocus', name);
      }, 100);
      scope.$on('$destroy', function () {
          $timeout.cancel(timer);
        }
      );
 
    }
  }
})();