Angular 2中的动态表单控制

时间:2018-07-16 10:55:41

标签: angular angular2-formbuilder

我正在使用Angular 2创建一个屏幕。它包含两个单选按钮A和B。在“ A”的勾选上,我需要显示下拉控件,在“ B”的勾选上,它应该显示文本框而不是下拉菜单。我关注了this博客,并根据该博客修改了我的代码。 现在一切正常,但是当我尝试使用prod标志进行构建时,出现以下错误: 类型“ AbstractControl”上不存在属性“控件” 关于单选按钮的更改,我要添加和删除控件。

2.txt

在HTML端

foo A.txt 2.txt
foo B.txt 2.txt
foo C.txt 2.txt

我在构建的1行HTML中遇到错误。我也尝试过其他方法

  handleChange(evt) {
    this.items = this.importExp.get('items') as FormArray;
    if (evt === this.database) {
      this.selectedApproach = this.database;
      this.delInput(0);
      this.items.push(this.createDbItem());
    } else {
      this.selectedApproach = this.file;
      this.delInput(0);
      this.items.push(this.createFileItem());
    }
  }

  delInput(index: number): void {
    const arrayControl = <FormArray>this.importExp.controls['items'];
    arrayControl.removeAt(index);
  }

  createForm() {
    this.importExp = this.fb.group({
      importFromDb: ['database'],
      items: this.fb.array([this.createDbItem()])
    });
  }

  createDbItem(): FormGroup {
    return this.fb.group({
      dbServer: ['', Validators.required],
    });
  }

  createFileItem(): FormGroup {
    return this.fb.group({
      fileTxt: ['', Validators.required],
    });
  }

但是它不起作用。

0 个答案:

没有答案
相关问题