Angular验证器不会被销毁。
创始人
2024-10-30 11:00:54
0

在Angular中,验证器通常是在模板驱动或反应式表单中使用的。验证器本身不会被销毁,但可以设置为在特定条件下失效。

下面是一个使用模板驱动表单的示例,其中包含一个自定义验证器:

import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-example',
  template: `
    
Field is required.
Field does not meet custom validation criteria.
` }) export class ExampleComponent { myForm: FormGroup; constructor() { this.myForm = new FormGroup({ myField: new FormControl('', [ Validators.required, this.customValidator ]) }); } customValidator(control: FormControl) { if (control.value !== 'valid') { return { customValidator: true }; } return null; } }

在上面的示例中,我们创建了一个自定义验证器customValidator,它检查表单控件的值是否为valid。如果验证失败,该验证器将返回一个对象,其中包含一个customValidator属性。如果验证通过,该验证器将返回null

注意,验证器本身不会被销毁。当表单控件的值更改时,验证器将被调用并重新评估。这意味着在每个表单控件的状态更改时,验证器都会被重新调用。

您可以根据需要在验证器中执行各种自定义逻辑,以满足您的验证需求。

相关内容

热门资讯

透视关于!hh poker辅助... 透视关于!hh poker辅助有用吗(透视)wepoker透视有吗,教程方针(有挂透明挂)-哔哩哔哩...
透视揭露!德州来玩辅助器(透视... 透视揭露!德州来玩辅助器(透视)hhpoker辅助挂是真的吗,教程绝活儿(有挂分析)-哔哩哔哩在进入...
透视有挂!wepoker私人局... 透视有挂!wepoker私人局辅助器怎么用(透视)hhpkoer辅助器视频,教程指南书(有挂秘笈)-...
透视推荐!aapoker透视怎... 透视推荐!aapoker透视怎么用(透视)智星德州插件2024最新版,教程法子(真的有挂)-哔哩哔哩...
透视普及!hhpoker透视码... 透视普及!hhpoker透视码(透视)aapoker公共底牌,教程模板(有挂规律)-哔哩哔哩1、上手...
透视揭露!hhpoker有没有... 透视揭露!hhpoker有没有开挂(透视)pokemmo辅助器手机版下载,教程步骤(有挂秘诀)-哔哩...
透视了解!hhpkoer辅助挂... 透视了解!hhpkoer辅助挂是真的吗(透视)哈糖大菠萝攻略,教程学习(证实有挂)-哔哩哔哩1、金币...
透视解迷!约局吧怎么看有没有挂... 透视解迷!约局吧怎么看有没有挂(透视)wepoker有没有机器人,教程窍门(确实有挂)-哔哩哔哩亲,...
透视推荐!wepokerplu... 透视推荐!wepokerplus透视脚本免费(透视)菠萝德州透视脚本,教程教材(存在有挂)-哔哩哔哩...
透视辅助!wepoker辅助器... 透视辅助!wepoker辅助器安装包(透视)wepoker插件程序激活码,教程阶段(有挂分析)-哔哩...