我试图将验证器方法传递给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
}
}
如何让装饰器将其参数传递给表单中的入口控件?
干杯
答案 0 :(得分:0)
尝试类似
的内容export function validators(...vals: Array<(...args: any[]) => void>) {
let form: FormGroup
return function <T extends object>(target: T, key: keyof T, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value
descriptor.value = function(...args: any[]) {
performValidation(...args);
originalMethod.call(target, ...args);
}
return descriptor;
}
}
泛型的原因仅仅是确保方法由目标对象定义。没有必要,但我发现它很有价值。
在调用方法时将验证数组vals
传递给框架更复杂。
最简单的方法是将验证器本身存储在类的一个字段中,以便它们可用,但这样做会破坏使用装饰器的目的。
例如我们可以写
export function validators(...vals: Array<(...args: any[]) => void>) {
return function <T extends {validators: Array<(...args: any[]) => void>}>(target: T) {
target.validators = vals;
}
}
这样我们的课就像
export class PimMatInputComponent {
validators: (...vals: Array<(...args: any[]) => void> = [];
@validators([PimNgxFormValidators.notEmpty])
ngOnInit() {
this.form = this._fb.group({
entry: [ '', [this.validators]]
});
this.entryCtrl = this.form.controls['entry'];
}
}
正如您所看到的,在这种情况下我们不会触及描述符,因为我们只是使用装饰器将验证器附加到类中。但这使得装饰者的使用变得多余,因为我们可以更容易地编写
export class PimMatInputComponent {
validators = [PimNgxFormValidators.notEmpty];
ngOnInit() {
this.form = this._fb.group({
entry: [ '', [this.validators]]
});
this.entryCtrl = this.form.controls['entry'];
}
}
方法装饰器对于验证包装方法体非常有用。通常它们可用于验证方法的参数,但它们不能将代码注入实现本身。
例如,如果我们希望验证传递给方法的参数,我们可以写一个像
这样的装饰器export function validateArguments(...validators: Array<(...as: any[]) => void>) {
return <T extends object>(target, key: keyof T, descriptor: PropertyDescriptor) => {
const originalMethod = descriptor.value;
descriptor.value = (...args: any[]) => {
args.forEach(argument => {
validators.forEach(validate => {
validate(argument);
});
});
originalMethod.call(target, ...args);
};
};
}
然后我们可以将它应用于任意方法并传递验证器,如下所示
export class A {
@validateArguments(arg => {
if (arg === undefined) {
throw Error('required parameter not specified');
}
})
m(a, b, c) {
console.log('If we got here no arguments were undefined.');
}
}