美文网首页
angular特殊字符指令

angular特殊字符指令

作者: 姜治宇 | 来源:发表于2024-12-17 11:34 被阅读0次

对于输入文本框,经常会有过滤特殊字符的要求,这个可以封装指令来解决。

import { Directive, ElementRef, HostListener } from '@angular/core';
import { NgControl } from '@angular/forms';
@Directive({
  selector: '[appFilterSpecialChar]'
})
export class FilterSpecialCharDirective {

  private specialStringRegexp = /[`~!@#$%^&*()+|{}\[\]:;'"<>?,.\/\\]/g;
  private isComposing = false;
  private curVal:any;
  constructor(private ngControl: NgControl,private el:ElementRef) {}
  ngOnInit(){
    const ctrl = this.ngControl.control;
    if(!ctrl)
      return;
    this.el.nativeElement.addEventListener('compositionstart',()=>{
      this.isComposing = true;
    });
    this.el.nativeElement.addEventListener('compositionend',()=>{
      this.isComposing = false;
    });
    ctrl.valueChanges.subscribe(v=>{
      if(!v || this.isComposing){
        return;
      }
      console.log('过滤前的字符>>>',v);
      this.curVal = v;
      this.cleanValue();
    });
  }
private cleanValue(){
  const ctrl = this.ngControl.control;
  if(ctrl){
    const filteredValue = this.curVal.replace(this.specialStringRegexp,'');
    console.log('过滤后的字符>>>',filteredValue);
    ctrl.setValue(filteredValue, { emitEvent: false });
  }
}
ngOnDestroy(){
  this.el.nativeElement.removeEventListener('compositionstart',()=>{
    this.isComposing = true;
  });
  this.el.nativeElement.removeEventListener('compositionend',()=>{
    this.isComposing = false;
    this.cleanValue();
  });
}
  //这样写会出现打汉字时,来不及拼出汉字就把拼音显示上去了
  // @HostListener('input', ['$event'])
  // onInput(event: Event): void {
  //   console.log('filter spec>>>',event);
  //   const input = event.target as HTMLInputElement;
  //   const filteredValue = input.value.replace(this.specialStringRegexp, '');
  //   this.ngControl.control?.setValue(filteredValue, { emitEvent: false });
  // }

}

指令主要用于封装对DOM的操作和行为,它们可以被附加到HTML元素、组件或属性上,用于低级别的DOM操作,如样式应用、事件监听、属性绑定等。
这里需要注意的是,如果直接监听input输入行为,会导致拼音输入法有问题,用户来不及拼出汉字就回显了,所以得用到compositionstart和compositionend事件。
当用户开始输入拼音时,触发compositionstart事件,此时可以设置一个状态标记表示正在输入法组合中。
当用户完成输入汉字后,触发compositionend事件,此时可以取消状态标记,并进行相应的处理。
然后在模块里面导入:

import { FilterSpecialCharDirective } from '../../directive/filter-special-char.directive';
import { SuffixInputComponent } from './suffix-input/suffix-input.component';
@NgModule({
  declarations: [
    FilterSpecialCharDirective,
    SuffixInputComponent
  ],
  imports: [
  ...
  ],
  exports: [],
})
export class TestModule {}

这样就可以在前端使用了:

    <input
      appFilterSpecialChar
      type="text"
      nz-input
      [placeholder]="text" 
      [(ngModel)]="keyword"
      (keyup.enter)="onSearch()"
      (ngModelChange)="trimValue($event)"
    />

相关文章

  • angular内置指令相关知识

    大纲 1、angular指令的分类2、angular指令之——组件3、angular指令之——属性指令 (ngSt...

  • 自定义指令(上)

    简介 在常用指令的章节中我们讲了Angular提供的指令,这些指令是Angular内部封装好指令,我们开箱...

  • Angular - 指令

    前言 使用指令的优势在于,我们无需太多关心指令的内部实现(当给 Angular 应用添加所需指令后,Angular...

  • Angular 指令,管道,服务

    1. 指令 Directive 指令是 Angular 提供的操作 DOM 的途径。指令分为属性指令和结构指令...

  • 2018-10-19 angular2+安装及常用npm指令

    一、angular2+安装步骤 二、angular2+ npm常用指令

  • Angular ngIf 跟他的新伙伴 else 和 then

    参考:https://angular.cn/api/common/NgIf Angular 扩展了ngIf 指令,...

  • AngularJS入门

    Angular应用程序启动 ng-app指令 指令指定Angular应用程序的根(root)元素,用于启动Angu...

  • angular笔记三

    angular第三天 1.自定义指令 官方都是以ng-开头的指令,而angular内部可以通过自定义指令来创建属于...

  • 重构过程中的信息记录二

    Angular深入理解之指令 angular 事件绑定/属性绑定 @HostListener ,@HostBind...

  • ng事件指令

    概述 Angular JS对各种事件进行了对应指令的封装,使其能够更好的配合Angular JS使用。具有ng指令...

网友评论

      本文标题:angular特殊字符指令

      本文链接:https://www.haomeiwen.com/subject/rydlejtx.html