Javascript 为什么我的输入焦点指令不再有效?

Javascript 为什么我的输入焦点指令不再有效?,javascript,angularjs,events,input,angularjs-directive,Javascript,Angularjs,Events,Input,Angularjs Directive,我有一个指令,它假设一旦光标可见,就会自动将光标聚焦到输入中 此选项可以使用,但升级到Angular 1.4.1后不再有效 标记 <div class="sidebar" ng-controller="sidebar"> <div class="btn-search" ng-hide="showingTagSearchInput" ng-click="quickSearchTags()">Search</div> <

我有一个指令,它假设一旦光标可见,就会自动将光标聚焦到输入中

此选项可以使用,但升级到Angular 1.4.1后不再有效

标记

<div class="sidebar" ng-controller="sidebar">

  <div class="btn-search"
       ng-hide="showingTagSearchInput"
       ng-click="quickSearchTags()">Search</div>

  <div class="search-tags-input container-fluid" ng-show="showingTagSearchInput">
      <input type="text"
             focus-me="showingTagSearchInput"
             placeholder="search for a tag"
             ng-model="tagSearchInput"
             class="form-control">
  </div>
</div>
我的焦点指令

.directive('focusMe', ['$timeout', '$parse', function($timeout, $parse) {
    return {
        link: function(scope, element, attrs) {
            var model = $parse(attrs.focusMe);
            scope.$watch(model, function(value) {
                console.log('value ', value);
                console.log('element ', element);
                if (value === true) { 
                    $timeout(function() {
                        // element[0].focus(); 
                        element[0].onfocus = true; 
                    });
                }
            });
            element.bind('blur', function() {
                scope.$apply(model.assign(scope, false));
            })
        }
    }
}])
$timeout(function() {
    element[0].focus(); 
});

应该使用
元素[0].focus()
而不是
元素[0]。onfocus=true

元素[0]。focus()
将关注指令模板的第一个元素

代码

.directive('focusMe', ['$timeout', '$parse', function($timeout, $parse) {
    return {
        link: function(scope, element, attrs) {
            var model = $parse(attrs.focusMe);
            scope.$watch(model, function(value) {
                console.log('value ', value);
                console.log('element ', element);
                if (value === true) { 
                    $timeout(function() {
                        // element[0].focus(); 
                        element[0].onfocus = true; 
                    });
                }
            });
            element.bind('blur', function() {
                scope.$apply(model.assign(scope, false));
            })
        }
    }
}])
$timeout(function() {
    element[0].focus(); 
});

啊,太酷了,是的,这确实有效。。。但它在我的Angular应用程序中不起作用:(嗯,标记这个,可能会提出另一个问题。谢谢!@LeonGaban它应该以任何方式起作用。你在你的应用程序中尝试过这个代码吗?&plunkr是的,它在plunkr中起作用,但我的应用程序由于某种原因不起作用:(它也用……但我有一个更重要的问题,我会在一秒钟内发布一个新问题。你介意看一下这个问题吗?@LeonGaban当然……会的……谢谢:)