我有一个名为 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/