Using Angular 2 focus on next input field with enter key press
Asked Answered
R

3

4

I am new in angular 2. How can I move focus to next control on key enter in Angular 2. I implement this code but how not working properly. Please suggest me how I do this. Thanks

My component code

import { Component,Injectable,Inject,OnInit, OnDestroy, EventEmitter, Output, Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core';
import {Http, Response} from '@angular/http';
import {TestService} from '../../services/test.service';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';

import { RouterModule } from '@angular/router';
import { ActivatedRoute,Params } from '@angular/router';

@Component({
    selector : 'test_block',
    templateUrl : './partials/test_block.html',
    providers: [TestService]
})
@Directive({
    selector: '[test_block]'
})

export class TestComponent {


    private el: ElementRef;   
    @Input() test_block: any;
    branch_outstanding:any = [];
    charges:any = [];

    searched_charges:any = [];

    constructor(private test_service:TestService,  @Inject(ActivatedRoute) private route: ActivatedRoute, private _el: ElementRef,public renderer: Renderer) {        
        this.el = this._el;
    }

    @HostListener('keydown', ['$event']) onKeyDown(e:any) {
        if ((e.which == 13 || e.keyCode == 13)) {
            e.preventDefault();
            let control:any;
            control = e.srcElement.nextElementSibling;
            while (true){                
                if (control) {
                  if ((!control.hidden) && 
                     (control.nodeName == 'INPUT' || 
                      control.nodeName == 'SELECT' || 
                      control.nodeName == 'BUTTON' || 
                      control.nodeName == 'TEXTAREA'))
                     {
                            control.focus();
                            return;
                        }else{
                            control = control.nextElementSibling;
                        }                         
                }
                else {
                    console.log('close keyboard');
                    return;
                }            
            }
        }
    } 
}

Shows error in console:

Unhandled Promise rejection: Could not compile 'TestComponent' because it is not a component. ; Zone: ; Task: Promise.then ; Value: Error: Could not compile 'TestComponent' because it is not a component.

I don't know what is issue on this code. Please suggest me.

Robledo answered 17/4, 2017 at 5:13 Comment(8)
show your code, then only we can helpChelsea
I think it is because you are using two decorators for a single class.Uniseptate
where I change the code can you brief me @UniseptateRobledo
the question your refered is for a directive called onReturn, you have to define your own component(class) for test_block.Uniseptate
@Uniseptate I changed but still same errorRobledo
please post your newest code.Uniseptate
Let us continue this discussion in chat.Robledo
You shoul seperate your component and directive. Make a separate file for directive.Please Refer:linkFortuity
R
2

Finally got the solution

  1. I created two different decorators for TestComponent mention @Component and @Directive.
  2. After that @Directive I import in to my test.module.ts like this
    import { TestDirective } from './test.directive';
  3. It's very important in html bind <input test_block type="text"> if you not bind test_block then code not working properly
Robledo answered 17/4, 2017 at 11:58 Comment(0)
J
1

It could be lack of declaration, add TestComponent to declarations at app.module.ts

Jocko answered 17/4, 2017 at 8:35 Comment(0)
C
0

The usage is very limited. eg it will not work when using bootstrap css. because with renderer2, it is not possible to access childnodes

<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<input  type="number"  class="form-control" required [onReturn]>
</div>
<div class="col-md-4">
<input type="number"  class="form-control" required >
</div>
<div class="col-md-5">
<input type="number"  class="form-control" required >
</div>
<div class="col-md-3">
<input type="number"  class="form-control" required >
</div>
</div>
</div>
</div>
Chouinard answered 17/9, 2017 at 18:7 Comment(3)
Hi.have you found a solution that works with bootstrap?Raquel
At this time, I did not, but few people posted some ideas here github.com/angular/angular/issues/15674#issuecomment-333392750Chouinard
One solution could be using dependency injection and in this case my input fields would become a component I will be able to navigate through using dependency injection angular.io/guide/dependency-injection-navtreeChouinard

© 2022 - 2024 — McMap. All rights reserved.