代码之家  ›  专栏  ›  技术社区  ›  raju

成角嵌套窗体组6

  •  0
  • raju  · 技术社区  · 6 年前

    serviceItemForm: FormGroup;
    apiForm : FormGroup;
    
    this.serviceItemForm = this.fb.group({
      'name': new FormControl('', Validators.required),
      'description': new FormControl('', Validators.required),
      'categoryIds': new FormControl([], Validators.required),
      'tags': [],
      'status': '',
      'orderFormType': new FormControl([], Validators.required),
      'orderFormKey': new FormControl([], Validators.required),
      'workflow': [],
      'orderFormAction': new FormControl('', Validators.required),
      'customUI': new FormControl(''),
      'api': this.fb.group({
        'apiurl': new FormControl(''),
        'apimethod': new FormControl(''),
        'headers': this.fb.array([]),
        'payload': '',
      }),
      'userGroup': []
    });
    
    this.apiForm = this.serviceItemForm.get('api');
    

    在这里这个。apiForm给出的错误是这样的吗 Type 'AbstractControl' is not assignable to type 'FormGroup'. Property 'controls' is missing in type 'AbstractControl'. 在VSCode中。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Vikash Dahiya    6 年前

    您可以尝试以下代码:

    serviceItemForm: FormGroup;
    apiForm : FormGroup;
    
    this.apiForm = this.fb.group({
        'apiurl': new FormControl(''),
        'apimethod': new FormControl(''),
        'headers': this.fb.array([]),
        'payload': '',
      });
    
    this.serviceItemForm = this.fb.group({
      'name': new FormControl('', Validators.required),
      'description': new FormControl('', Validators.required),
      'categoryIds': new FormControl([], Validators.required),
      'tags': [],
      'status': '',
      'orderFormType': new FormControl([], Validators.required),
      'orderFormKey': new FormControl([], Validators.required),
      'workflow': [],
      'orderFormAction': new FormControl('', Validators.required),
      'customUI': new FormControl(''),
      'api': this.apiForm,
      'userGroup': []
    });
    
    
        2
  •  0
  •   Pardeep Jain    6 年前

    如果选中任何嵌套(或父级)的typeof FormGroup 它将始终返回object。所以你能做的就是像这样类型化你的变量-

    apiForm : Object;
    ...
    this.apiForm = this.serviceItemForm.get('api');
    

    否则,您可以在为这样的变量赋值时进行类型转换-

    apiForm : FormGroup;
    this.apiForm = (<FormGroup>this.serviceItemForm.get['api']);