daniellmb/AQUA

View on GitHub
demos/angularjs/js/directives/todoFocus.js

Summary

Maintainability
A
1 hr
Test Coverage
/*global angular */

/**
 * Directive that places focus on the element it is applied to when the
 * expression it binds to evaluates to true
 */
angular.module('todomvc')
    .directive('todoFocus', function todoFocus($timeout) {
        'use strict';

    /**
     * linking function
     * @param {Object} scope
     * @param {Object} elem
     * @param {TodoEscape} attrs
     */
        return function (scope, elem, attrs) {
            scope.$watch(attrs.todoFocus, function (newVal) {
                if (newVal) {
                    $timeout(function () {
                        elem[0].focus();
                    }, 0, false);
                }
            });
        };
    });