【问题标题】:Angular ControlValueAccessor onChanges not updating form control valueAngular ControlValueAccessor onChanges 不更新表单控件值
【发布时间】:2019-05-29 17:27:43
【问题描述】:

这里是复制的组件/问题的stackblitz

我已经构建了一个自定义输入组件:

  1. 采用用户对象数组进行预输入过滤
  2. 将您的选择显示为标签
  3. 表单控件值是选定用户的数组users[]

问题是adding a result(input-tags.component)没有更新表单(app.component),我不明白为什么。

input-tags.component.ts

addTag(contact: any) {
   ...
   this.onChange(this.tags); // update controller value
}

app.component.ts

this.form.controls['users'].valueChanges.subscribe(data => {
  this.control = data; // always null
});

onChanges 按预期调用,一切正常,除了表单控件始终为空。为什么?

【问题讨论】:

    标签: angular forms onchange


    【解决方案1】:

    错误似乎在您的addTag() 函数中。您正在尝试访问 typeaheadSource 中不存在的参数。将 contact.userId 更改为 contact.id 就可以了。

    【讨论】:

      猜你喜欢
      • 2023-02-05
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 2017-03-15
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多