首页 > 解决方案 > Angular ControlValueAccessor onChanges 不更新表单控件值

问题描述

这是复制的组件/问题的堆栈闪电。

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

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

问题是添加结果(input-tags.component)不会更新表单(app.component),我不明白为什么。

输入标签.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按预期调用,一切正常,除了表单控件始终为空。为什么?

标签: angularformsonchange

解决方案


错误似乎在您的addTag()函数中。您正在尝试访问您的typeaheadSource. 更改contact.userIdcontact.id,您应该一切顺利。


推荐阅读