javascript - autoFocus 指令在 Firefox 中不起作用

标签 javascript angularjs

我编写的指令在 Firefox(版本 36.00)中不起作用。

这应该与 html 5 中的 atuofocus 属性相同。

这是代码:

app.directive('autoFocus', function($timeout) {
    return {
        restrict: 'AC',
        link: function(_scope, _element) {
            $timeout(function(){
                _element[0].focus();
            }, 0);
        }
    };
});  

DEMO

有什么想法吗?谢谢

最佳答案

我遇到了和你一样的问题,对于 Firefox,你需要一个解决方法,将其包裹在 watch 中:

    _scope.$watch('autoFocus', function (value) {
        if (value) {
            _element[0].focus();
        }
    });

这肯定可以解决您的问题。

关于javascript - autoFocus 指令在 Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29850865/

相关文章:

php - 如何记录 JavaScript 操作?

javascript - 如何使用 $routeProvider 在 Angular 中更改页面标题

javascript - 如何访问 $http 调用的 .success 中的类属性?

angularjs - 使用Nginx作为代理以避免CORS

c# - 为什么 C# 中不存在提升?

javascript - react : set the state based on array values

javascript - 在 Javascript Controller 中使用 AngularJS 表达式

javascript - 在 $state.go 之后执行 $window.location.reload(true)

javascript - 用 d3 制作日历

javascript - 不使用 Fieldset 的 AngularJS NgDisabled