Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在formArray中实现价值_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 如何在formArray中实现价值

Javascript 如何在formArray中实现价值,javascript,angular,typescript,Javascript,Angular,Typescript,我对formarray的patchvalue有一个问题,它一直显示一个空数组。 我希望能够将视频和文档的URL分别修补到formBuilder中的视频和文档formarray中,但它不起作用 .ts文件 ngOnInit() { this.uploadForm = this.formBuilder.group({ uploadName: ['',Validators.required], uploadPrice: ['',Validators.required]

我对formarray的patchvalue有一个问题,它一直显示一个空数组。 我希望能够将视频和文档的URL分别修补到formBuilder中的视频和文档formarray中,但它不起作用

.ts文件

 ngOnInit() {
    this.uploadForm = this.formBuilder.group({
      uploadName: ['',Validators.required],
      uploadPrice: ['',Validators.required],
      uploadAuthor: ['',Validators.required],
      uploadDes: ['',Validators.required],
      uploadImage: ['',Validators.required],
      numberofVideo: [''],
      videos: new FormArray([]),
      numberofDoc: [''],
      documents: new FormArray([])
    })
  }
  get f(){
    return this.uploadForm.controls;
  }
  get t(){
    return this.f.videos as FormArray;
  }
  get x(){
    return this.f.documents as FormArray;
  }

  onSubmit(){
    this.submitted = true;
    console.log('click')
    if(this.uploadForm.invalid){
      return;
    }
    this.uploadForm.patchValue({
      uploadImage:this.imageUrl,
      uploadVideo:this.videoUrls,
      uploadDocument:this.pdfUrls
    })
    console.log(this.uploadForm.value);
    this.itemService.addItem(this.uploadDetail);
    Object.keys(this.uploadForm.controls).forEach(key => {
      this.uploadForm.get(key).setErrors(null) ;
    });
  }

  onChangeVideo(e){
    const numberOfVideo = e.target.value || 0;
    if(this.t.length < numberOfVideo){
      for(let i=this.t.length;i<numberOfVideo;i++){
        this.t.push(this.formBuilder.group({
          uploadVideo: new FormControl('')
        }));
      }
    }else{
      for(let i = this.t.length; i >= numberOfVideo;i--){
        this.t.removeAt(i)
      }
    }
  }

  onChangeDoc(e){
    const numberOfDoc = e.target.value || 0;
    if(this.x.length < numberOfDoc){
      for(let i=this.x.length;i<numberOfDoc;i++){
        this.x.push(this.formBuilder.group({
          uploadDocument:new FormControl('')
        }));
      }
    }else{
      for(let i = this.x.length; i >= numberOfDoc;i--){
        this.x.removeAt(i)
      }
    }
  }

你试着用另一种方法来实现你的价值

this.uploadForm.get('uploadImage').setValue(this.imageUrl);
this.uploadForm.get('uploadVideo').setValue(this.videoUrls);
this.uploadForm.get('uploadDocument').setValue(this.pdfUrls);
你能试试这个吗

this.uploadForm.get('videos').setValue(this.videoUrls);
this.uploadForm.get('documents').setValue(this.pdfUrls);
还是这个

this.uploadForm.patchValue({
 videos:this.videoUrls,
 documents:this.pdfUrls
});

你没有表单控件uploadVideo和uploadDocument,你把它命名为videos和Documents我只是更新了代码你可能误解了FormArray到底是什么。它不应该将数据存储为数组,而是存储FormControls的另一种方式。事实上,FormArray与FormGroup相同,只是它不是FormControls的对象,而是FormControls的数组。你真的需要正式的安排吗?否则,您也可以使用FormControl来存储任何类型的数据数组。
this.uploadForm.patchValue({
 videos:this.videoUrls,
 documents:this.pdfUrls
});