import { Injectable } from '@angular/core'; import { FormControl } from '@angular/forms'; import { NaValidators } from './na-validators'; @Injectable({ providedIn: 'root' }) export class NaForm { constructor() {} /** * 可为空 * * @param {*} [defaultValue] * @returns {FormControl} * @memberof NaForm */ empty(defaultValue?: any): FormControl { return new FormControl(defaultValue, { validators: [] }); } /** * 必输字段 * * @param {*} [defaultValue=null] * @returns {FormControl} * @memberof NaForm */ required(defaultValue: any = null): FormControl { return new FormControl(defaultValue, { validators: [ NaValidators.required, ] // 报告更新策略AbstractControl(意味着控件更新自身的事件) 'change' | 'blur' | 'submit' Default value: 'change' // updateOn: 'blur' }); } /** * 密码 * * @param {*} [defaultValue=''] * @returns {FormControl} * @memberof NaForm */ password(defaultValue: any = ''): FormControl { return new FormControl(defaultValue, { validators: [ NaValidators.required, NaValidators.password(defaultValue) ] }); } }