简体   繁体   中英

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. But I am not able to set the focus on 1 of the elements of this.elem.nativeElement .

I want to focus on 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. Here is the link: Angular directive to set an element focus via the controller

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). Then in the controller use $broadcast to set focus on that element. You have to inject the ngSetFocus service in your controller.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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