2017-12-12 19 views
1

저는 Angular Reactive 양식을 작성 중입니다. 이건 내 구성 요소 클래스 코드입니다 :FormControl 값이 배열 일 수 있는지 여부

ngOnInit(){ 
     this.reviewForm = this.fb.group({    
      'controlArray': new FormArray([]) 
     });   
     this.showDefaultForm();      
    } 

addForm() { 
    (<FormArray>this.reviewForm.get('controlArray')).push(this.fb.group({ 
     controlType: control.controlType, 
     options: control.options, 

    })); 
} 

나는이 오류가 발생했습니다. this.validator는 이와 관련된 기능이 아닙니다. 나는 FormControl에 문자열 배열 (예 : control.options)을 값으로 할당한다고 생각합니다. FormArray로 만들면이 오류가 해결되지만 템플릿의 FormArray로 처리하는 데 문제가 있습니다. FormArray를 사용하지 않는 경우, FormControl에 문자열 배열을 값으로 할당 할 수 있습니까? 그렇다면 템플릿에서 FormArray로 처리하는 방법. 감사합니다

답변

1

마지막으로 나는 그것을 해결하고 대답은 예입니다. FormControl의 값은 배열이 될 수 있습니다. 그러나 이것을 위해서는 값을 대괄호 []로 묶어야합니다. 또한 간단한 값 (비 배열)의 경우 대괄호는 선택 사항이므로 대괄호 안의 값을 묶을 수 있습니다.

코드 설명 :

(<FormArray>this.reviewForm.get('controlArray')).push(this.fb.group({ 
     controlType: control.controlType, 
     options: [control.options], //previously I was not using square brackets with options value i.e. options: control.options which was wrong. 

    }));