在angularjs的某个字段上设置条件焦点 [英] set conditional focus on some field in angularjs

查看:223
本文介绍了在angularjs的某个字段上设置条件焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,所有工程师,我已经看到了很多问题,并且很好地提出了关于将注意力集中在某些输入字段上的建议,但是我没有找到任何满足我要求的东西,现在我的问题是我们是否有多个输入字段,而我们不知道在哪里设置焦点,并且在某些条件下我想在控制器返回的某个字段上设置焦点,是否有类似"document.getElementbyId(").focus"的内容,如带有"ng-model"的javascript "之类的东西,感谢您的任何帮助

Hello all fellow engineers , i have seen so many questions and well suggesting answers regarding setting focus on some input field but i didn't found anything which will fulfill my requirements , now my question is if we have multiple input fields and we don't know where to set the focus , and on some condition basis i want to set focus on some field back from controller, is there anything like "document.getElementbyId("").focus" as in javascript with "ng-model" or something , any help will be appreciated thanks

推荐答案

当传入的表达式为true时,此伪指令将使元素获得焦点.

This directive will cause an element to receive focus when the passed in expression is truthy.

用法:

<!-- set `$scope.foo` to a truthy value when this input should be focused -->
<input type="text" focus-when="foo">
<button ng-click="foo = true">Focus input.</button>

指令:

.directive('focusWhen', function() {
  return {
    scope: {
      focusWhen: '='
    },
    link: function($scope, $element) {

      $scope.$watch('focusWhen', function(shouldFocus) {
        if (shouldFocus) {
          $element[0].focus();
        }
      });

    }
  };
})

这篇关于在angularjs的某个字段上设置条件焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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