首页 > 解决方案 > 找不到带有路径的控件:'files -> 0 -> postId'

问题描述

我需要使用 formArray 创建一个动态表单。

我单击AddItem并且必须为上传文件创建输入。

在 HTML 中使用此代码:

    <div class="row m-auto col-md-12 pb-4">
    <div class="col-md-2 col-xs-2 col-sm-2 col-lg-2 col-xl-2">
        <button (click)="AddItem()" mat-stroked-button color="warn">
            <i class="la la-plus"></i>
            <label class="pr-2 pl-2">
                ایجاد فایل جدید
            </label>
        </button>
    </div>
</div>
<mat-divider></mat-divider>
<div class="row m-auto col-md-12 pt-2">
    <form [formGroup]="uploadFormGroup">
        <div class="left row m-auto col-md-12 col-xs-12 col-sm-12 col-lg-12 col-xl-12" formArrayName="files"
            *ngFor="let project of uploadFormGroup.get('files').controls; let i = index">
            <ng-container [formGroupName]="i">
                <div class="col-md-2 col-lg-2 col-xl-2 col-sm-12">
                    <mat-form-field class="mat-form-field-fluid" formControlName="postId" appearance="outline">
                        <mat-label>{{ 'POST.FILE_TYPE' | translate }}</mat-label>
                        <mat-select>
                            <mat-option *ngFor="let item of fileType | enumToArray" [value]="item">
                                {{ 'ENUM.FILE_TYPE.'+item | translate }}
                            </mat-option>
                        </mat-select>
                    </mat-form-field>
                </div>
                <div class="col-md-5 col-lg-5 col-xl-5 col-sm-5">
                    <mat-form-field appearance="outline">
                        <mat-label>{{ 'GENERAL.PHOTO' | translate }}</mat-label>
                        <mat-icon matSuffix>image</mat-icon>
                        <ngx-mat-file-input appearance="outline" formControlName="file" type=file
                            [placeholder]="'GENERAL.PHOTO' | translate">
                        </ngx-mat-file-input>
                    </mat-form-field>
                </div>
                <div class="row m-auto col-md-5 col-sm-5 col-lg-5 col-xl-5 col-lg-5 pb-4">
                    <div class="col-md-3 col-xs-3 col-sm-3 col-lg-3 col-xl-3">
                        <button mat-flat-button color="warn" (click)="removeItem(i)">حذف</button>
                    </div>
                    <div class="col-md-4 col-xs-4 col-sm-4 col-lg-4 col-xl-4">
                        <button mat-flat-button color="primary">پیش نمایش</button>
                    </div>
                    <div class="col-md-3 col-sm-3 col-lg-3 col-xl-3">
                        <button mat-flat-button color="accent" (click)="Upload(i)">آپلود</button>
                    </div>
                </div>
            </ng-container>
        </div>
    </form>
</div>

和 ts 文件中的这段代码:

  @Input() private postId: number;

  uploadFormGroup: FormGroup;
  fileType = TypeFile;
  fileUpload: FileUpload;

  constructor(private formBuilder: FormBuilder, private postService: PostService) { }

  ngOnInit(): void {
    this.uploadFormGroup = this.formBuilder.group({
      postId: [''],
      files: this.formBuilder.array([]),
      typeEnum: ['', Validators.compose([Validators.required])]
    });
  }

  createItem(): FormGroup {
    return this.formBuilder.group({
      file: ['']
    });
  }

  removeItem(index: number): void {
    const Item = <FormArray>this.uploadFormGroup.controls['files'];
    Item.removeAt(index);
  }

  AddItem(): void {
    const Item = <FormArray>this.uploadFormGroup.controls['files'];
    console.log(Item);
    Item.push(this.createItem());
  }

  Upload(fileId: number): void {

    this.fileUpload.postId = this.postId;
    this.fileUpload.typeEnum = this.uploadFormGroup.controls['typeEnum'].value;
    this.fileUpload.files = <FormArray>this.uploadFormGroup.controls['files'][fileId].value;
    this.postService.UplaodFile(this.fileUpload).subscribe(data => {
      console.log(data);
    });
  }

}

现在,当我单击AddItem()它时,它会显示此错误:

找不到带有路径的控件:'files -> 0 -> postId'

有什么问题 ????

标签: javascriptangulartypescript

解决方案


请再次检查您的代码 postId 控件在 uploadFormGroup 下而不是在文件 formArray 下,并且您正在尝试访问文件 formArray 下的 postId

您必须在文件 formArray 中添加 postId 或在 HTML 中的 formArray 循环之外访问它

createItem(): FormGroup {
 return this.formBuilder.group({
  file: new FormControl(),
  postId: new FormControl()
 });
}

推荐阅读