使用 Angular 2 使用回车键聚焦下一个输入字段
Using Angular 2 focus on next input field with enter key press
我是 angular 的新手 2. 如何在 Angular 中将焦点移至下一个输入键的控件 2. 我 implement this 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;
}
}
}
}
}
在控制台中显示错误:
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.
我不知道这段代码有什么问题。请建议我。
可能是缺少声明,在 app.module.ts
的声明中添加 TestComponent
终于找到解决方案
- 我为 TestComponent 提到 @Component 和 @Directive 创建了两个不同的装饰器。
- 在@Directive 之后,我像这样
导入我的test.module.ts
import { TestDirective } from './test.directive';
- 在 html 绑定中非常重要
<input test_block type="text">
如果你不绑定 test_block
那么代码将无法正常工作
使用非常有限。例如,当使用 bootstrap css 时它将不起作用。
因为使用 renderer2,无法访问子节点
<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>
我是 angular 的新手 2. 如何在 Angular 中将焦点移至下一个输入键的控件 2. 我 implement this 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;
}
}
}
}
}
在控制台中显示错误:
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.
我不知道这段代码有什么问题。请建议我。
可能是缺少声明,在 app.module.ts
的声明中添加 TestComponent终于找到解决方案
- 我为 TestComponent 提到 @Component 和 @Directive 创建了两个不同的装饰器。
- 在@Directive 之后,我像这样
导入我的test.module.ts
import { TestDirective } from './test.directive';
- 在 html 绑定中非常重要
<input test_block type="text">
如果你不绑定test_block
那么代码将无法正常工作
使用非常有限。例如,当使用 bootstrap css 时它将不起作用。 因为使用 renderer2,无法访问子节点
<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>