angular中如何将子组件的FormControl绑定到父组件的FormGroup中?

angular初学者一枚,现在有一个表单方面的需求
我使用PrimeNg的DropDown自己写了一个包含内容下拉组件

<p-dropdown [options]="cities" [formControlName]="tzformControlName"></p-dropdown>
import {Component, Input, OnInit} from '@angular/core';
import {SelectItem} from 'primeng/primeng';
import {FormGroup} from "@angular/forms";

@Component({
  selector: 'tz-dropdown-car',
  templateUrl: './dropdown-car.component.html',
  styleUrls: ['./dropdown-car.component.css']
})
export class DropdownCarComponent implements OnInit {
  cities: SelectItem[];
  @Input()
  tzformControlName: string;

  constructor() {
    this.cities = [];
    this.cities.push({label: 'Select City', value: null});
    this.cities.push({label: 'New York', value: {id: 1, name: 'New York', code: 'NY'}});
    this.cities.push({label: 'Rome', value: {id: 2, name: 'Rome', code: 'RM'}});
    this.cities.push({label: 'London', value: {id: 3, name: 'London', code: 'LDN'}});
    this.cities.push({label: 'Istanbul', value: {id: 4, name: 'Istanbul', code: 'IST'}});
    this.cities.push({label: 'Paris', value: {id: 5, name: 'Paris', code: 'PRS'}});
  }

  ngOnInit() {
  }

}

然后我想直接放到父组件的响应式表单中

clipboard.png

我试了将FormControlName传参到子组件,但是提示找不到父FormGroup

请问该如何配置?

阅读 8.5k
1 个回答

将父组件的formGroup传递到子组件的属性中。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进