博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular2 Form] Angular 2 Template Driven Form Custom Validator
阅读量:4957 次
发布时间:2019-06-12

本文共 1221 字,大约阅读时间需要 4 分钟。

In this tutorial we are going to learn how we can also implement custom form field validation in Angular 2 template driven forms, by creating our own custom validation directive. We are going to see how to write such directive and how its a best practive to extract the validation function to a separate function, so that it can also be used for model driven validation.

We are going to learn how we can configure the template driven custom validator directive into the Angular 2 Forms mechanism, by plugging the directive into the dependency injection system using NG_VALIDATORS and forwardRef.

 

import {Validator, NG_VALIDATORS, FormControl} from "@angular/forms";import {validateDuration} from "./validateDuration";import {Directive, forwardRef} from "@angular/core";export const MIN_LENGTH_VALIDATOR = {  provide: NG_VALIDATORS,  multi: true,  useExisting: forwardRef(() => DurationValidator)};// target and duration with ngModel@Directive({  selector: '[duration][ngModel]',  providers: [MIN_LENGTH_VALIDATOR]})export class DurationValidator implements Validator {  validate(c: FormControl): {[key: string]:any} {    return validateDuration(c);  }}

 

use:

Duration: 

 

转载于:https://www.cnblogs.com/Answer1215/p/6008975.html

你可能感兴趣的文章
Python开发基础 day12 模块2
查看>>
Socket编程 (异步通讯,解决Tcp粘包) - Part3
查看>>
OSI七层协议
查看>>
Linux 空闲空间的格式化与加载
查看>>
Linux 终端 Bash 常用快捷键介绍及经验
查看>>
关于twitter的GIF变mp4的测试
查看>>
Android中通过typeface设置字体
查看>>
android 优秀框架整理
查看>>
java线程中的interrupt,isInterrupt,interrupted方法
查看>>
where can I find source of com.android.internal.R.styleable.AlertDialog_multiChoiceItemLayout?
查看>>
ViewDragHelper详解
查看>>
Akka(43): Http:SSE-Server Sent Event - 服务端主推消息
查看>>
Developing for nRF52810(转载)
查看>>
java netty nio
查看>>
115 不同的路径Ⅱ
查看>>
POJ2104 K-th Number 主席树
查看>>
【转】Spring学习---为什么要用spring,springMVC
查看>>
idea激活
查看>>
wmi文章地址
查看>>
盒子的偏移量
查看>>