angular6.x 동적 양식 FormArrayName
주요 사고방식 이용this.fb.array 선점formControl
추가할 때 formArray 재구성 - set Control 다시 수정 Control
전시 코드는 2층formArray 플러그인 코드로 좀 복잡하고 완전하지 않은 점 양해해주세요~
htmlformArrayName formGroupName의 사용을 주의하시면 됩니다.
formArrayName 은 formControl 의 이름입니다.
formGroupName은 동적array의 다음 표입니다. 왜냐하면formArray 집합에 있는 모든 대상은 다음과 같은 표로 구분된 다음 표는formGroup을 대표합니다.
tip:
formGroup의 get 방법은 get('aa.bb')을 통해 컨트롤의 하위 컨트롤을 얻을 수 있습니다
ts
  addName() {
        this.show = false;
        let attr = this.commonForm.get("attributeGroups") as FormArray;
        let next = 9999
        if (attr.length != 0) {
            attr.get(attr.length - 1 + "").patchValue({
                next: attr.length
            })
        }
        let attributeNameaa = this.fb.group({
            groupName: [''],
            lock: [false],
            attributes: this.fb.array([this.fb.group({
                    attrValue: ''
                })]
            ),
            next: next
        })
        if (this.attrArryForm.length == 0) {
            attr.push(attributeNameaa)
            this.attrArryForm = this.commonForm.get("attributeGroups") as FormArray
            console.log(this.attrArryForm);
        } else {
            attr.push(attributeNameaa)
            attr = this.commonForm.get("attributeGroups") as FormArray;
            if (attr.value[attr.length - 2].attrName == '') {
                attr.removeAt(this.attrArryForm.length - 1);
                this.attrArryForm = this.commonForm.get("attributeGroups") as FormArray
                console.log(this.attrArryForm);
                this.dialogService.toast('warning', '  ', '      ');
                return;
            } else {
                attr = this.commonForm.get("attributeGroups") as FormArray;
                if (attr.value[attr.length - 2].attributes[0].attrValue == '') {
                    attr.removeAt(this.attrArryForm.length - 1);
                    this.attrArryForm = this.commonForm.get("attributeGroups") as FormArray
                    console.log(this.attrArryForm);
                    this.dialogService.toast('warning', '  ', '      ');
                    return;
                }
            }
        }
    }
    addValue(i) {
        this.show = false;
        let attributeValues = this.fb.group({
            attrValue: ''
        });
        let attr = this.commonForm.get("attributeGroups") as FormArray;
        let newVar = attr.get(i + ".attributes") as FormArray;
        newVar.push(attributeValues)
        if (newVar.value[newVar.length - 2].attrValue == '') {
            newVar.removeAt(newVar.length - 1)
            this.dialogService.toast('warning', '  ', '       ');
        }
        this.attrArryForm = this.commonForm.get("attributeGroups") as FormArray
    }
    /**
     *      (       )
     * @param i(     )
     */
    deleteName(i) {
        this.show = false;
        let attr = this.commonForm.get("attributeGroups") as FormArray;
        attr.removeAt(i)
        this.attrArryForm = this.commonForm.get("attributeGroups") as FormArray
    }
    /**
     *      
     * @param i(     )
     * @param j(     )
     */
    deleteValue(i, j) {
        this.show = false;
        let attr = this.commonForm.get("attributeGroups") as FormArray;
        let newVar = attr.get(i + ".attributes") as FormArray;
        newVar.removeAt(j)
        this.attrArryForm = this.commonForm.get("attributeGroups") as FormArray
    }html
 
                
                    
                    
                        
                        
                        
                        
                        
                            
                            
                                        
                                        
                                    
                                
                        
                    
                
            
            
                
                
            이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.