捕获ng-show事件和焦点输入字段

use*_*172 3 javascript jquery angularjs

我不想写复杂的指令只是为了集中注意力.如果方法将使用纯js,则可以接受.我的问题是如何捕捉角度的ng-show.

<a ng-click="showInput=true">show</a>
<input type="text" ng-show="showInput" />
Run Code Online (Sandbox Code Playgroud)

上面的代码显示输入,如何将其集中在它的外观上?

小智 5

您可以添加此指令:

app.directive('showFocus', function($timeout) {
  return function(scope, element, attrs) {
    scope.$watch(attrs.showFocus, 
      function (newValue) { 
        $timeout(function() {
            newValue && element.focus();
        });
      },true);
  };    
});
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

<input type="text" ng-show="showInput" show-focus="showInput">
Run Code Online (Sandbox Code Playgroud)