【问题标题】:Show input and autofocus显示输入和自动对焦
【发布时间】:2014-12-17 13:14:13
【问题描述】:

点击时我有一个链接,它必须显示另一个输入和自动对焦。

<a ng-click="ctrl.switchAndFocus()" href="#">FOCUS</a>

在控制器中我有

this.switchAndFocus= function  (){
        if (this.controler.data.radioOne == 'one') {
            this.controler.data.radioTwo = "kmh";
            angular.element("#high").focus();
        }
        else{
            this.controler.data.radioTwo = "kmh";
            angular.element("#low").focus();
        }
    }

(this.controler.data.radioOne 是值为'one'的单选按钮,this.controler.data.radioTwo 是值为'kmh'的单选按钮,angular.element("#high")-> 来自输入字段的id )

这可行,但我必须点击链接 2 次。任何想法如何使它在 1 点击切换和聚焦?

【问题讨论】:

标签: angularjs autofocus


【解决方案1】:

https://github.com/hiebj/ng-focus-if

http://plnkr.co/edit/MJS3zRk079Mu72o5A9l6?p=preview

<input focus-if />

(function() {
    'use strict';
    angular
        .module('focus-if', [])
        .directive('focusIf', focusIf);

    function focusIf($timeout) {
        function link($scope, $element, $attrs) {
            var dom = $element[0];
            if ($attrs.focusIf) {
                $scope.$watch($attrs.focusIf, focus);
            } else {
                focus(true);
            }
            function focus(condition) {
                if (condition) {
                    $timeout(function() {
                        dom.focus();
                    }, $scope.$eval($attrs.focusDelay) || 0);
                }
            }
        }
        return {
            restrict: 'A',
            link: link
        };
    }
})();

【讨论】:

    猜你喜欢
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 2018-11-24
    • 2017-06-11
    • 1970-01-01
    相关资源
    最近更新 更多