我正在尝试创建一个自定义指令来验证多个文件上传。但在自定义指令中,控件只是返回最后一个文件的详细信息而不是数组。
下面是代码:
文件上传.html:
<form [formGroup]="validateDocumentForm">
<input formControlName="document" style="display: none" type="file" multiple (change)="onFileChanged($event)" #fileInput accept="application/pdf" class="form-control">
<button class="btn upload-doc-btn" (click)="fileInput.click()"><span><i class="material-icons">vertical_align_top</i> Upload File</span></button>
文件上传.ts
ngOnInit() {
this.validateDocumentForm = this.formBuilder.group({
document: this.formBuilder.array(['', [
CustomValidator.uploadDocument
]]),
});
}
自定义验证器.ts:
export class CustomValidator {
static uploadDocument(control: AbstractControl):
ValidationErrors | null {
console.log(control); // only last file's details instead of
array of selected files.
console.log(control.value);
return null;
}
}