Setting focus on an input field after ng-show

后端 未结 5 795
遥遥无期
遥遥无期 2020-12-13 13:41

Is it possible set the focus of an input field via a controller once data has loaded (via $resource in this case)?

The input is hidden via ng-show until the data is

5条回答
  •  猫巷女王i
    2020-12-13 14:33

    Building on koolunix's answer, if you're only looking to focus input elements it may also be useful to find the first input element that is a child of the element with the focus-on directive.

    This is useful in the case of an external library abstracting the input element with directives and templates.

    .directive('focusOn', function ($timeout) {
        return {
            restrict: 'A',
            priority: -100,
            link: function ($scope, $element, $attr) {
    
                $scope.$watch($attr.focusOn,
                    function (_focusVal) {
                        $timeout( function () {
                            var inputElement = $element.is('input')
                                ? $element
                                : $element.find('input');
    
                             _focusVal ? inputElement.focus()
                                       : inputElement.blur();
                        });
                    }
                );
    
            }
        };
    });
    

提交回复
热议问题