export class InputXXXDirective {

  constructor(private elementRef: ElementRef, private control: NgControl) { }

  @HostListener('keydown', ['$event'])
  keydownFun(evt) {
    if (evt.key && evt.key.trim() === '') {
      const u = navigator.userAgent;
      const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios终端:拼音通过空格分词(需要兼容)
      if (!isiOS) {
        evt.preventDefault();
      }
    }
  }

  @HostListener('keyup', ['$event', '$event.target'])
  keyupFun(evt, target) {
    if (evt.key && evt.key.trim() === '') {
      const u = navigator.userAgent;
      const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios终端:拼音通过空格分词(需要兼容)
      if (isiOS) {
        this.keychangeFun(evt, target);
      }
    }
  }

  @HostListener('change', ['$event', '$event.target'])
  keychangeFun(evt, target) {
    const reg = /(\s+)/g;
    if (target.value && reg.test(target.value)) {
      this.control.control.setValue(target.value.replace(/(\s+)/g, ''));
      target.focus();
    }
  }

}

因为苹果IOS系统的中文输入法,是通过空格分隔拼音,如果去空格会打断中文输入法,造成输入混乱,所以需要进行兼容处理。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐