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.
onReturn
, you have to define your own component(class) fortest_block
. – Uniseptate