gpt4 book ai didi

javascript - 呈现动态输入数组

转载 作者:行者123 更新时间:2023-11-30 19:27:08 24 4
gpt4 key购买 nike

我正在尝试呈现一个动态的 FormArray(单击“+”时它应该添加一个新的),但是当我将一些文件放入输入框中时总是会出现消息(“Nenhum Arquivo Selecionado”,意思是“文件没有't Exist”)停留在屏幕上。

enter image description here

但是,如果我检查 this.filterForm.get('Documents') 上的信息,则该行已正确填充。

有没有人有解决这个错误的建议?

协议(protocol).component.ts

items: FormArray;

filterForm = new FormGroup({
IdProtocolo: new FormControl(),
Documentos: this.formBuilder.array([ this.createItem() ]
);

ngOnInit() {
this.items = this.filterForm.get('Documentos') as FormArray;
}

createItem(): FormGroup{
return this.formBuilder.group({
filename: '',
filetype: '',
value: ''
})
}

addItem(){
this.items.push(this.createItem());
}

removeItem(index){
if(this.items.length > 1) {
this.items.removeAt(index);
}
}

onFileChange(event: any, index: any) {
let reader = new FileReader();

if(event.target.files && event.target.files.length > 0) {
let file = event.target.files[0];
reader.readAsDataURL(file);
this.items.at(index).patchValue({
filename: file.name,
filetype: file.type,
value: (reader.result as string).split(',')[1]
})
}
}

协议(protocol).component.html

<div *ngFor="let item of filterForm.value.Documentos; let i = index;">
<div class="row" style="margin-bottom: 10px;">
<div class="col-md-4">
<input type="file" formControlName="Documentos" (change)="onFileChange($event, i)">
</div>
<div class="col-md-8">
<button class="btn btn-success-tce" (click)="addItem()">+</button>
<button class="btn btn-success-tce" (click)="removeItem(i)"style="margin-left: 5px">-</button>
</div>
</div>

最佳答案

[更新] formArray 的实现可能有误。我在您的模板中看不到 formArrayName。我会这样实现的

在你的模板中

<p> Dynamic File Form </p>
<form [formGroup]="someForm" (submit)="formSubmit()">
<div formArrayName="documents">
<div *ngFor="let item of files?.controls; let i = index;">
<input type="file" placeholder="Upload file" [formControlName]="i" (change)="onFileChange($event, i)"/>
</div>
</div>
<button type="submit"> Submit </button>
</form>

<button type="button" (click)="addFileControl()"> Add File </button>

在你的组件中。

  initForm() {
this.someForm = this.fb.group({
documents: this.fb.array([this.fileControl])
})
}

get files() {
return this.someForm.get('documents') as FormArray;
}

get fileControl() {
return this.fb.group({
file_item: [null]
})
}

addFileControl() {
this.files.push(this.fileControl);
}

formSubmit() {
console.log(this.someForm.value);
}

onFileChange(event, i) {
let reader = new FileReader();

if (event.target.files && event.target.files.length) {
const [file] = event.target.files;
reader.readAsDataURL(file);

reader.onload = () => {
this.files.controls[i].get('file_item').setValue(reader.result);

// need to run CD since file load runs outside of zone
this.cd.markForCheck();
};
}
}

这是 stackblitz例子。这将为您提供 base64 格式的输出,但您也可以通过修改以文件格式获取它。

onFileChange(event, i) {
if (event.target.files && event.target.files.length) {
this.files.controls[i].get('file_item').setValue(event.target.files;);
}
}

注意:- 这只是一个粗略的代码,但可以完成工作:)。

关于javascript - 呈现动态输入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56790473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com