必要字段的Angular 2指令不起作用

时间:2016-11-28 08:00:20

标签: angular angular2-directives

为所需字段创建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;
}

}

0 个答案:

没有答案