angularjs – 输入自动对焦属性
发布时间:2020-12-17 08:23:21 所属栏目:安全 来源:网络整理
导读:我在我的代码中有我的地方: input data-ng-disabled="SOME_SCOPE_VARIABLE" / 我也希望能够像这样使用它: input data-ng-autofocus="SOME_SCOPE_VARIABLE" / 或者甚至更好,模仿ng风格: input data-ng-attribute="{autofocus: SOME_SCOPE_VARIABLE}" / 这
我在我的代码中有我的地方:
<input data-ng-disabled="SOME_SCOPE_VARIABLE" /> 我也希望能够像这样使用它: <input data-ng-autofocus="SOME_SCOPE_VARIABLE" /> 或者甚至更好,模仿ng风格: <input data-ng-attribute="{autofocus: SOME_SCOPE_VARIABLE}" /> 这是否存在于当前版本的AngularJS?我在代码中注意到有一个BOOLEAN_ATTR,它获得了AngularJS支持的所有attr。我不想修改,因为担心更改版本,忘记更新。
更新:AngularJS现在有一个
ngFocus 指令,用于评估焦点上的表达式,但是为了完整起见,我在这里提到。
当前版本的AngularJS没有焦点指令,但它在路线图中。巧合的是,我们昨天在邮件列表上是talking about 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 ); } }); } }; }); 根据您的要求,它适用于范围变量: <input type="text" ng-focus="isFocused" ng-focus-lost="loseFocus()"> 这是一个小提琴:http://jsfiddle.net/ANfJZ/39/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |