【发布时间】:2018-08-21 14:24:45
【问题描述】:
如何在 Angular 的验证指令中触发 ngOnChanges 的验证函数?
我检测到 ngOnChanges,但它无法触发验证功能
@Directive({
selector: '[uppercase]',
providers: [{
provide: NG_VALIDATORS,
useExisting: RdUppercaseDirective,
multi: true
}]
})
export class RdUppercaseDirective implements Validator, OnChanges {
@Input('uppercase') uppercase: any;
r = new rdValidators;
validate(control: AbstractControl): {
[key: string]: any
} | null {
let u = this.uppercase === 'false' || this.uppercase === false ? false : true;
if(!control.value)
{
return null;
}
if(u === false)
{
return null;
}
var result = (/[a-z]/.test(control.value));
return control.dirty && control.value ? result ? { 'uppercase' : true } : null : null;
}
ngOnChanges(changes: SimpleChanges){
if(changes.uppercase){
//**How to Trigger Validate Function Here!**
}
}
}
【问题讨论】:
-
你必须有控制参考才能做到这一点。
-
如何使用控件引用
-
你必须以某种方式提供它。我怎么会知道?我什至不知道该指令的上下文。 idownvotedbecau.se/nomcve
标签: angular angular6 angular-validation angular-validator