如何将焦点设置为div元素内的锚标签,div元素具有ng-show

时间:2016-07-01 04:48:04

标签: angularjs anchor ng-show

<div   class="test" ng-show="model.data.IsDataLoaded" tabindex="50">
  <a href="" id="id1" ng-click="testData($event)">Clear all</a>
</div>

//在js中我试图通过下面的代码设置焦点

$scope.model.data.IsDataLoaded = true;
 angular.element("#id1").focus();

1 个答案:

答案 0 :(得分:1)

将您的ng-show更改为ng-if并使用指令:

JS:

.directive('autoFocus', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element[0].focus();
        }
    }
});

HTML:

<div class="test" ng-if="model.data.IsDataLoaded" tabindex="50">
    <a href="" id="id1" ng-click="testData($event)" auto-focus>Clear all</a>
</div>