我正在尝试使用装饰器将验证器方法传递到 angular4 反应形式以进行验证。
零件
@AutoUnsubscribe( [] )
@Component(
{
selector: 'pim-mat-input',
template: `
<form
autocomplete = 'off'
[formGroup] = 'form'>
<section
fxLayout = 'column'
fxLayoutAlign = 'start start'>
<mat-form-field>
<input
#input
matInput
formControlName = 'entry'>
</mat-form-field>
</section>
</form>
`,
styles: []
} )
export class PimMatInputComponent implements AfterViewInit, OnDestroy, OnInit {
protected entryCtrl: AbstractControl
protected form: FormGroup
constructor( private _fb: FormBuilder ) {
}
@validators( [ PimNgxFormValidators.notEmpty ] )
ngOnInit() {
this.form = this._fb.group(
{ entry: [ '', [ /*PimNgxFormValidators.notEmpty goes here*/] ] } )
this.entryCtrl = this.form.controls[ 'entry' ]
}
ngOnDestroy() {
// needed for @AutoUnsubscribe()
}
ngAfterViewInit(): void {
}
}
装饰器尝试 - 不完整
export function validators( vals: Array<Function> = [] ) {
let form: FormGroup
return function( target: any, member: string, descriptor: PropertyDescriptor ) {
const originalMethod = descriptor.value
descriptor.value = function( ...args ) {
// What should I put here to allow the argument to be used in the component
}
return descriptor
}
}
如何让装饰器将其参数传递给表单中的入口控件?
干杯