在查看了如何使用角度设置焦点元素的示例后,我看到它们中的大多数使用一些变量来监视然后设置焦点,并且它们中的大多数对于要设置焦点的每个字段使用一个不同的变量。在具有许多字段的形式中,这意味着存在许多不同的变量。
考虑到jquery的方式,但是想以角度的方式做到这一点,我提出了一个解决方案,我们使用元素的id将焦点设置在任何函数上,因此,由于我在角度上非常陌生,因此我希望得到一些意见那个方法是正确的,有任何问题,任何可以帮助我更好地做到这一点的方法。
基本上,我创建了一个指令,该指令监视用户使用指令定义的范围值或默认的focusElement,并且当该值与元素的id相同时,该元素集将自动聚焦。
angular.module('appnamehere') .directive('myFocus', function () { return { restrict: 'A', link: function postLink(scope, element, attrs) { if (attrs.myFocus == "") { attrs.myFocus = "focusElement"; } scope.$watch(attrs.myFocus, function(value) { if(value == attrs.id) { element[0].focus(); } }); element.on("blur", function() { scope[attrs.myFocus] = ""; scope.$apply(); }) } }; });
由于某种原因需要引起关注的输入将采用这种方式
<input my-focus id="input1" type="text" />
这里有任何要设置焦点的元素:
<a href="" ng-click="clickButton()" >Set focus</a>
和设置焦点的示例函数:
$scope.clickButton = function() { $scope.focusElement = "input1"; }
那是一个好的角度解决方案吗?我的糟糕经历是否会带来一些我还没有看到的问题?
您的解决方案的问题在于,当与其他创建新作用域的指令(例如)捆绑在一起时,它不能很好地工作ng- repeat。更好的解决方案是简单地创建一个服务函数,使您可以在控制器中强制性地集中元素或在html中声明性地集中元素。
ng- repeat
演示
JAVASCRIPT
服务
.factory('focus', function($timeout, $window) { return function(id) { // timeout makes sure that it is invoked after any other event has been triggered. // e.g. click events that need to run before the focus or // inputs elements that are in a disabled state but are enabled when those events // are triggered. $timeout(function() { var element = $window.document.getElementById(id); if(element) element.focus(); }); }; });
指示
.directive('eventFocus', function(focus) { return function(scope, elem, attr) { elem.on(attr.eventFocus, function() { focus(attr.eventFocusId); }); // Removes bound events in the element itself // when the scope is destroyed scope.$on('$destroy', function() { elem.off(attr.eventFocus); }); }; });
控制者
.controller('Ctrl', function($scope, focus) { $scope.doSomething = function() { // do something awesome focus('email'); }; });
HTML
<input type="email" id="email" class="form-control"> <button event-focus="click" event-focus-id="email">Declarative Focus</button> <button ng-click="doSomething()">Imperative Focus</button>