Angular2 patchValue push value into array

前端 未结 4 450
南笙
南笙 2020-12-30 02:41

Is it It looks like Angular2\'s FormGroup.patchValue() doesn\'t push new elements into an array.

For example something like this:

ngOnInit() {

             


        
4条回答
  •  轮回少年
    2020-12-30 03:16

    I hope this will help you. I have a complex object where my object has an object inside and object inside. sorry for my grammar. but i hope my code will help you

    ngOnInit() {
        this.descriptifForm = this._fb.group({
          name: 'ad',
          descriptifs: this._fb.array([ this.buildForm() ]),
        });
    
        this._service.getData().subscribe(res => {
          this.descriptifForm.setControl('descriptifs', this._fb.array(res || []));
        });
    
      }
    buildA(): FormGroup {
        return this._fb.group({
          Id: '',
          Groups: this._fb.array([ this.buildB() ]),
          Title: ''
        });
      }
    
      buildB(): FormGroup {
        return this._fb.group({
          Id: '',
          Fields: this._fb.array([ this.bbuildC() ]),
          Type: ''
        });
      }
    
      buildC(): FormGroup {
        return this._fb.group({
          Answers: this._fb.array([ this.buildD() ]),
          Code: '',
        });
      }
    
      buildD(): FormGroup {
        return this._fb.group({
          Data: ''
        });
      }
    

提交回复
热议问题