为所需字段创建Angular 2指令,该字段未按预期工作
我需要实现的功能是,如果该指令作为属性包含在模板输入中,则必须检查该字段是否为空,如果为空则必须抛出错误
我的代码
import {Directive, Attribute, forwardRef} from '@angular/core';
import {NG_VALIDATORS,AbstractControl,Validator} from '@angular/forms';
@Directive({
selector : '[requiredValidators][formControlName],[requiredValidators][formControl],[requiredValidators][ngModel]',
providers : [
{provide:NG_VALIDATORS,useExisting:forwardRef(()=>requiredValidations),multi :true}
]
})
export class requiredValidations implements Validator {
constructor(@Attribute('requiredValidators')public requiredValidators:string){}
validate(c:AbstractControl):{[key: string]:any}{
let inputValue = c.value;
if(inputValue == "" || inputValue == null){
}
return null;
}
}