问题
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.
回答1:
Finally got the solution
- I created two different decorators for TestComponent mention @Component and @Directive.
- After that @Directive I import in to my
test.module.ts
like thisimport { TestDirective } from './test.directive';
- It's very important in html bind
<input test_block type="text">
if you not bindtest_block
then code not working properly
回答2:
It could be lack of declaration, add TestComponent to declarations at app.module.ts
回答3:
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>
来源:https://stackoverflow.com/questions/43445507/using-angular-2-focus-on-next-input-field-with-enter-key-press