简体   繁体   English

专注于当前的活动元素

[英]focus on the current active element

I want to focus on an element based on its native element. 我想关注基于其本机元素的元素。

I am using Angular 2, this.elem.nativeElement gives me the entire required details. 我正在使用Angular 2, this.elem.nativeElement为我提供了所需的全部详细信息。 But I am not able to set the focus on 1 of the elements of this.elem.nativeElement . 但是我无法将焦点放在this.elem.nativeElement的元素之一this.elem.nativeElement

I want to focus on id="dateEdit" . 我想专注于id="dateEdit"

Kindly help me. 请帮助我。

<div class="mydp" id="dtpid">
    <div class="selectiongroup">
        <div [formGroup]="form" class="ntt">
            <input type="text" id="{{controlName}}" [formControlName]="controlName" (click)="openBtnClicked()" (blur)="changeValue($event.currentTarget.value)" class="datePickAtUnique selection form-control" [ngClass]=" 
        {'invaliddate': invalidDate&&opts.indicateInvalidDate}" placeholder=" 
        {{opts.showDateFormatPlaceholder? 
        opts.dateFormat:opts.customPlaceholderTxt}}">
        </div>
        <span id="datePickAT" class="selbtngroup" [style.height]="opts.height">
        <button type="button" id="dateEdit" aria-label="Clear Date" 
        class="btnclear" *ngIf="selectionDayTxt.length>0" 
         (click)="removeBtnClicked()" [ngClass]="{'btnclearenabled': 
       !opts.componentDisabled, 'btncleardisabled': opts.componentDisabled}" 
       [disabled]="opts.componentDisabled">
          <span class="icon icon-cross" [ngStyle]="{'line-height': 
          opts.height}"></span>
        </button>
        <button type="button" id="dateChange" aria-label="Open Calendar" class="btnpicker" (click)="openBtnClicked()" [ngClass]=" 
        {'btnpickerenabled': !opts.componentDisabled, 'btnpickerdisabled': 
         opts.componentDisabled, 'disabled': disabled}" [disabled]="opts.componentDisabled">
            <span class="icon icon-calendar" [ngStyle]="{'line-height': 
             opts.height}"></span>
        </button>
        </span>
    </div>

There is a github repo by Chris Turnbull. 克里斯·特恩布尔(Chris Turnbull)提供了一个github仓库。 Here is the link: Angular directive to set an element focus via the controller 这是链接: 通过控制器设置元素焦点的Angular指令

Include this into your project. 将此包含到您的项目中。 Basically what you have to do is use ng-set-focus attribute on any element that you want to set focus on and give it a value(has to be unique). 基本上,您要做的就是在要设置焦点的任何元素上使用ng-set-focus属性,并为其赋予一个值(必须是唯一的)。 Then in the controller use $broadcast to set focus on that element. 然后在控制器中使用$ broadcast将焦点设置在该元素上。 You have to inject the ngSetFocus service in your controller. 您必须在控制器中注入ngSetFocus服务。

Example: 例:

In dom:
    <button ng-click="setFocusHere('button1')">Button 1</button>
    <button ng-click="setFocusHere('button2')">Button 2</button>

In controller:
     <script>
  angular.module('ngSetFocusDemo', ['ngSetFocus'])
    .controller('DemoController', function($scope) {

      $scope.setFocusHere = function(el){
        console.log(el)
        $scope.$broadcast(el);
      }

    })
</script>

Working example here 这里的工作示例

As my code shows, there are 2 buttons and I wanted the focus on the first button with id="dateEdit". I had said that .focus() always points to the first element with this id, it was because I was calling the .focus() before the element even loaded, for this I changed my angular 2 condition from "ngIf" to "hidden". I came up with this, that helped me,

var x = this.elem.nativeElement.getElementsByTagName("button")[0];
setTimeout(function(){
x.focus();
},100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM