我写了一个基本的角度指令来关注元素。它在不同的页面中工作正常,但它在我使用Bootstrap组件创建的模态对话框中不起作用。
我的指示是:</ p>
myApp.directive('focus', function () {
return {
restrict: "A",
link: function (scope, element, attrs) {
console.log('directive ' + element);
angular.element(element).focus();
}
};
});
感谢您的帮助
答案 0 :(得分:2)
myApp.directive('focus', function () {
return {
restrict: "A",
link: function (scope, element, attrs) {
console.log('directive ' + element);
setTimeout(function(){angular.element(element).focus();}, 200);
}
};
当您尝试设置焦点时,该元素尚不可见
答案 1 :(得分:0)
我有类似的情况,发现原生HTML autofocus
属性就像魅力一样。
<button autofocus>Example</button>