<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();
答案 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>