在元素获得焦点后触发单击以激活预输入

Trigger click to activate typeahead after element gets focus

需要帮助解决几个问题。这是简化的代码:

HTML

    <input type="text" title="{{e.Name}}" ng-model="e.modelName" ng-required="true" typeahead-editable="false" ng-blur="vm.isUnchanged(i)" focus-me="vm.Event"
own-typeahead typeahead-on-select="vm.changeValue($item, $model, $label, i)" 
uib-typeahead="$Event, $viewValue)" typeahead-min-length="0"/>

JS

app.directive("ownTypeahead", function() {
        var directive = {
            link: link,
            scope: true,
            restrict: 'A',
            require: ["ngModel"]
        };
        return directive;

        function link(scope, element, attrs, ctrls) {

            element.bind('click', function () {
                if (ctrls[0].$viewValue && ctrls[0].$viewValue == ' ') {
                    ctrls[0].$setViewValue('');
                }
                ctrls[0].$setViewValue(' ');
            });

            element.bind('focus', function () {
                if (!ctrls[0].$viewValue || ctrls[0].$viewValue == '') {
                    ctrls[0].$setViewValue(' ');
                }
            });
        }
    });


/**
 * Directive that places focus on the element it is applied to when the
 * expression it binds to evaluates to true
 */
app.directive('focusMe', ['$timeout', function focusMe($timeout) {
    return function (scope, elem, attrs) {
        scope.$watch(attrs.focusMe, function (newVal) {
            if (newVal) {
                $timeout(function () {
                    elem[0].focus();
                }, 0, false);
            }
        });
    };
}]);

problems/questions 是:

1) 主要的。单击某些项目后获得焦点几乎总是会在输入字段中触发 typeahead ddl,但有几个项目会移动焦点但不会触发列表打开。任何想法问题在哪里? (上面的代码在大约 90% 的情况下有效,在单击输入字段后在 100% 的情况下有效)

2) 不是理想的解决方案,但可以在聚焦的输入字段上触发 click 事件以打开列表。无法以 angular 的方式做到这一点。这怎么能做到?

通过为 focusMe 添加 200ms 超时使其工作:

app.directive('focusMe', ['$timeout', function focusMe($timeout) {
    return function (scope, elem, attrs) {
        scope.$watch(attrs.focusMe, function (newVal) {
            if (newVal) {
                $timeout(function () {
                    elem[0].focus();
                }, 200, false);
            }
        });
    };
}]);

如果谁有更好的建议会采纳答案。