angular中如何实现一个多层复杂的表单

import { Component, OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-store-generate',
  templateUrl: './store-generate.component.html',
  styleUrls: ['./store-generate.component.css']
})
export class StoreGenerateComponent implements OnInit{
  
  validateForm:FormGroup
  activeTab="indoor"
  tabs=['indoor']
  sizeTypeList=[{name:"小桌",value:'small'},{name:"中桌",value:'middle'},{name:"大桌",value:'large'}]
  constructor(private fb:FormBuilder){
    this.validateForm=this.fb.group({
      storename:[''],
      tableSize:this.fb.group({
          toggle:['true'],
          groupSize:this.fb.array([
            this.fb.group({
              cate:['indoor'],
              title_en:[''],
              title_zh:[''],
              tableSize:this.fb.array([
                this.fb.group({
                  size: ["large"],
                  desc_en:["Large"],
                  desc_zh: ["大桌"],
                  number:['A01'],
                  preTitle: ["C"],
                  maxPerson: [8],
                  minPerson: [5]
                }),
                this.fb.group({
                  size: ["large"],
                  desc_en:["Large"],
                  desc_zh: ["小桌"],
                  number:['A02'],
                  preTitle: ["C"],
                  maxPerson: [8],
                  minPerson: [5]
                })
              ])
            }),
            this.fb.group({
              cate:['outdoor'],
              title_en:[''],
              title_zh:[''],
              tableSize:this.fb.array([
                this.fb.group({
                  size: ["large"],
                  desc_en:["Large"],
                  desc_zh: ["大桌"],
                  number:['B01'],
                  preTitle: ["C"],
                  maxPerson: [8],
                  minPerson: [5]
                }),
            
              ])
            }),
          ]),
      }),
      geoFancing:this.fb.group({
        isOpenGeo:['true'],
        range:[100]
      }),
      dynamicQRCode:['true'],
      updatefrequency:[3]

    })
  }
  ngOnInit(){

    this.validateForm.get("isNeed")?.valueChanges.subscribe(value=>{
      console.log("value",value)
    })

  }
 



  get groupSize(): FormArray {
    let gp=this.validateForm.get('tableSize.groupSize') as FormArray;
    console.log('gp:',gp)
    return this.validateForm.get('tableSize.groupSize') as FormArray;
  }

  tableSizeControls(index:number): FormArray {
    let list=this.validateForm.get('tableSize.groupSize') as FormArray;
    let lastlist=list.controls[index].get('tableSize') as FormArray ||[]
    console.log("lastlist:",lastlist)
    return lastlist
  }

  }

  changeTab(type:string){
      this.activeTab=type
  }
  
  submitForm(){
    console.log("this.form",this.validateForm.value)
  }
}


--------------------------------以下为html对应的结构----------------------------------
店铺名称 tale size & group size
Add Remove
Geo-dncing Geo-dncing-range KM
Geo-dncing-range Update frequency KM

你可能感兴趣的:(angular,前端,javascript,a,angular)