0

我目前为应用程序中的所有文本框编写了一个 scrollIntoView 指令。将指令添加到应用程序中的数千个文本框是很痛苦的。

是否有任何拦截器类型的东西可以拦截文本框的所有焦点事件。

4

1 回答 1

0

我尝试了这样的事情并且它有效。

import { Directive, OnInit, HostListener } from '@angular/core';

@Directive({
  selector: "[input[type='text']]"
})
export class InputTypeDirective implements OnInit{
  ngOnInit(){
    console.log('loading directive');
  }

  @HostListener('focus', ['$event'])
  onFocus(e){
    console.log(e);
  }
}

模板:

<input type="text" [(ngModel)]="username" value="">

还将指令作为声明包含在模块中。

于 2018-10-09T13:45:31.617 回答