Angular模式验证器在失焦时停止调用
创始人
2024-10-28 15:31:20
0

在Angular中,可以使用自定义指令来实现当输入框失焦时停止调用模式验证器的功能。下面是一个示例:

import { Directive, ElementRef, HostListener } from '@angular/core';
import { NgControl } from '@angular/forms';

@Directive({
  selector: '[stopValidationOnBlur]'
})
export class StopValidationOnBlurDirective {

  constructor(private el: ElementRef, private ngControl: NgControl) { }

  @HostListener('blur')
  onBlur() {
    // 停止调用模式验证器
    this.ngControl.control?.markAsUntouched();
  }
}

在上面的示例中,我们创建了一个名为stopValidationOnBlur的自定义指令。该指令通过ElementRefNgControl获取了当前输入框元素和与输入框绑定的表单控件。

通过@HostListener装饰器,我们监听了输入框的blur事件,并在事件触发时调用onBlur方法。在该方法中,我们使用markAsUntouched方法将表单控件标记为未触摸状态,从而停止调用模式验证器。

要使用这个自定义指令,只需将它应用到需要停止调用模式验证器的输入框元素上,例如:


这样,当输入框失焦时,模式验证器将不再被调用。

相关内容

热门资讯

透视存在!aapoker怎么控... 透视存在!aapoker怎么控制牌(透视)透视脚本(都是有挂)1)aapoker怎么控制牌辅助挂:进...
透视神器!werplan有挂吗... 透视神器!werplan有挂吗(透视)AI教程(2020已更新)(哔哩哔哩)1、下载好werplan...
透视透视!aapoker怎么设... 透视透视!aapoker怎么设置抽水(透视)透视方法(确实有挂)1、aapoker怎么设置抽水系统规...
透视真的!xpoker辅助神器... 透视真的!xpoker辅助神器(透视)AI教程(2025已更新)(哔哩哔哩);1、xpoker辅助神...
透视存在!aapoker辅助怎... 透视存在!aapoker辅助怎么用(透视)透视软件(好像存在有挂)1、起透看视 aapoker辅助怎...
透视代打!werplan有挂吗... 透视代打!werplan有挂吗(透视)软件教程(2023已更新)(哔哩哔哩)1、让任何用户在无需we...
透视安卓版!aapoker透视... 透视安卓版!aapoker透视怎么用(透视)透视脚本入口(果然是有挂);所有人都在同一条线上,像星星...
透视app!sohoo辅助(透... 透视app!sohoo辅助(透视)揭秘教程(2020已更新)(哔哩哔哩);1、下载好sohoo辅助辅...
透视模拟器!aapoker透视... 透视模拟器!aapoker透视脚本(透视)透视脚本下载(切实是有挂)1、首先打开aapoker透视脚...
透视科技!pokemmo辅助器... 透视科技!pokemmo辅助器脚本下载(透视)黑科技教程(2024已更新)(哔哩哔哩);一、poke...