Angular 6 Dynamic Forms issue [part 2]

放肆的年华 提交于 2019-12-13 07:54:17

问题


Following recommendations from Angular 6 Dynamic Forms issue I've made my app to print one field out, but only one empty and with an error

ERROR Error: Cannot find control with name: 'field0'

I've placed my code here https://stackblitz.com/edit/angular-mzu1ab

I'm generating field names by indices, since the data that I fetch contains no name fields.


回答1:


This happens because you create your form control names dynamically, but you don't really bind them to your questions.

Instead of chosing an arbitrary name like field + i, you should instead bind on a specific value.

In this stackblitz, I binded the form control names to the label, and as you can see, it works like a charm.

<input [formControlName]="question.label"
questsions.forEach((e, i) => {
  if (e.type === 'string' || e.type === 'text' || e.type === 'list') {
    group[e.label] = new FormControl(e.value, Validators.required);


来源:https://stackoverflow.com/questions/51762013/angular-6-dynamic-forms-issue-part-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!