forms - 如何在 formArray 中设置值

标签 forms angular

我有一个名为 input form 的表单组,其中包含一系列主题。我想使用动态创建的过滤列表更改输入字段的值。

过滤函数工作正常:

filtertopic(idx: number) {
  const test = this.topics.value[idx].topic;
  if (test !== "") {
    this.onderdeel = "topics";
    this.queryselector = idx;
    this.filteredList = this.Topics.filter(function(el) {
      return el.toLowerCase().indexOf(test.toLowerCase()) >-1;
    }.bind(this));
     } else { this.filteredList = [];
    }
    }

但更改输入字段值的函数 handleBlur 不起作用

handleBlur() {
    console.log(this.selectedIdx);
    if (this.selectedIdx > -1) {
      if (this.onderdeel =="topics") {
        this.topics.value[this.queryselector].topic.setValue(this.filteredList[this.selectedIdx]);
      } else {
        this.query = this.filteredList[this.selectedIdx];
      }
    }
    this.filteredList = [];
    this.selectedIdx = -1;
}

我认为这与

有关

this.topics.value[this.queryselector].topic.setValue(this.filteredList[this.selectedIdx]);

设置表单控件的值。有人知道解决方案吗?

最佳答案

 this.yourdataobject.forEach(task => {
      this.formcontrolname.push(
        this.fb.group({
          name: [task.name, Validators.required]
        })
      );
    });

关于forms - 如何在 formArray 中设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44524794/

相关文章:

python - 如何通过 Client.post() 从 Django 测试中的表单上传文件?

javascript - 有规范的 meteor.js 表单包吗?

jquery - 如何正确发送带有文本和文件输入字段的表单数据?

angular - Ionic 4 自定义环境文件

rest - 如何在不将其转换为字符串或 json 的情况下访问 Angular 2 http 响应主体?

python - 验证 django 内联表单集

javascript - 将 PHP 变量转换为 JavaScript 变量

javascript - 在服务器上提交后如何重新加载组件?

angular - 具有 angular-auth-oidc-client 的 Angular 8 应用程序的 Azure B2C - b2clogin 端点 POST CORS 错误

angular - 如何在静态函数中调整大小/更改时将 gridster-item 保存在数据库中?