输入自动对焦属性 [英] Input autofocus attribute

查看:322
本文介绍了输入自动对焦属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的code的地方,我有这样的:

I have places in my code where I have this:

<input data-ng-disabled="SOME_SCOPE_VARIABLE" />

我希望能够使用它像这样太:

I would like to be able to use it like this too:

<input data-ng-autofocus="SOME_SCOPE_VARIABLE" />

甚至更好,模仿NG风格是如何做的:

Or even better, mimicking how ng-style is done:

<input data-ng-attribute="{autofocus: SOME_SCOPE_VARIABLE}" />

这是否在AngularJS的当前版本存在吗?我在code注意到有它可以获取所有ATTR的是AngularJS支持一个BOOLEAN_ATTR。我不想修改,在不断变化的恐惧版本,并忘记更新的。

Does this exist in the current version of AngularJS? I noticed in the code there's a BOOLEAN_ATTR which gets all the attr's that AngularJS supports. I don't want to modify that in fear of changing versions and forgetting to update.

推荐答案

更新:现在AngularJS拥有的 ngFocus 指令,计算一个前pression的的焦点,但我在这里提到它的缘故完整。

Update: AngularJS now has an ngFocus directive that evaluates an expression on focus, but I mention it here for the sake of completeness.

AngularJS的当前版本不具有焦点的指令,但它的路线图。巧合的是,我们说起这在邮件列表昨天,我想出了这个:

The current version of AngularJS doesn't have a focus directive, but it's in the roadmap. Coincidentally, we were talking about this on the mailing list yesterday, and I came up with this:

angular.module('ng').directive('ngFocus', function($timeout) {
    return {
        link: function ( scope, element, attrs ) {
            scope.$watch( attrs.ngFocus, function ( val ) {
                if ( angular.isDefined( val ) && val ) {
                    $timeout( function () { element[0].focus(); } );
                }
            }, true);

            element.bind('blur', function () {
                if ( angular.isDefined( attrs.ngFocusLost ) ) {
                    scope.$apply( attrs.ngFocusLost );

                }
            });
        }
    };
});

其中一期工程为关您请求的范围的变量:

Which works off a scope variable as you requested:

<input type="text" ng-focus="isFocused" ng-focus-lost="loseFocus()">

下面是一个小提琴: http://jsfiddle.net/ANfJZ/39/

这篇关于输入自动对焦属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆