使用 Angular 2 将焦点放在下一个输入字段并按下 Enter 键 [英] Using Angular 2 focus on next input field with enter key press

查看:20
本文介绍了使用 Angular 2 将焦点放在下一个输入字段并按下 Enter 键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 angular 2 的新手.如何在 Angular 2 中将焦点移到下一个控制键输入.我 实现此代码但如何无法正常工作.请建议我如何做到这一点.谢谢

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

我的组件代码

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

在控制台中显示错误:

未处理的承诺拒绝:无法编译TestComponent",因为它不是组件.;区域: ;任务:Promise.then;值:错误:无法编译TestComponent",因为它不是组件.

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. 我为 TestComponent 提到了 @Component 和 @Directive 创建了两个不同的装饰器.
  2. 在@Directive之后,我像这样导入到我的test.module.ts
    import { TestDirective } from './test.directive';
  3. 在html中绑定<input test_block type="text">很重要,如果你没有绑定test_block那么代码就不能正常工作
  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

这篇关于使用 Angular 2 将焦点放在下一个输入字段并按下 Enter 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆