访问嵌套的FormBuilder Angular6

我有一个简单的反应形式

public static void readInputFile()
    {
        //Create a new instance of the array class so that the array can be
        //written to without creating a new array for either merge or insert
        //sort
        Array arr = new Array(1000010);

        Element[] array = arr.getarray();

        try
        {
            fileRead = new FileReader(fileLocation);
            br = new BufferedReader(new FileReader(fileLocation));

            //Allows the first line of the file that includes the titles to 
            //not be added to the array
            String line = br.readLine();

             //Counter for iterating through array
                int elemCntr = 0;
            while (elemCntr != 10)
            {
                //Variables
                String lineline = br.readLine();



                //This will split the line into sections based on the 
                //placement of comas: split data into sections
                String[] readLine = lineline.split(",");
                firstName = readLine[0];
                lastName = readLine[1];
                company = readLine[2];
                address = readLine[3];
                city = readLine[4];
                county = readLine[5];
                state = readLine[6];
                zip = Integer.parseInt(readLine[7]);
                phone = readLine[8];
                key = Long.parseLong(readLine[9]);
                rowNum = arr.numElements + 1;

                //Insert data into array
                arr.insertvalue(firstName,lastName,company,address,city,state,county,phone,zip,key,rowNum);
             }
         } catch (Exception e){
          // do something
         }
 }

我正在尝试访问

this.orderForm = this._formBuilder.group({
    metadataFormGroup: this._formBuilder.group({
      data1: ['',[Validators.pattern(this.nserRegex)]],data2: ['',[Validators.pattern(this.crqRegex)]]
    }),infoFormGroup: this._formBuilder.group({
      data3: ['',[Validators.pattern(this.blcRegex)]],data4: new FormControl([])
    })
  });

但这给this.orderForm['metadataFormGroup'].controls['data1'].setvalue('11111111')

带来了错误

请帮助

ac2234 回答:访问嵌套的FormBuilder Angular6

您需要通过controlsthis.orderForm.controls['metadataFormGroup']...访问嵌套组。但是,我认为更干净的解决方案是:

this.orderForm.get('metadataFormGroup').get('data1').setValue('...');
,

您可以通过

设置metadataFormGroup的值
this.orderForm.setValue({
  ...this.orderForm.value,metadataFormGroup: {
    data1,data2
  }
});

您可以通过以下方式获取metaFormFormGroup的值

this.orderForm.value.metadataFormGroup
,

您可以使用setValue对其进行更新

  

setValue()方法可为单个控件设置新值。   setValue()方法严格遵守表单的结构   分组并替换控件的整个值。

 this.orderForm.get('metadataFormGroup').get('data1').setValue('Testing');

这里get方法在给定控件名称或路径的情况下检索子控件。

Here is the working demo

,

尝试使用get方法

this.orderForm.get('metadataFormGroup').get('data1').setValue('11111111');

使用get属性可以简化上面的行


get  metadataFormGroup(){
  return this.orderForm.get('metadataFormGroup');
}

然后您可以像这样设置值

this.metadataFormGroup.get('data1').setValue('11111111');
本文链接:https://www.f2er.com/2916036.html

大家都在问