【发布时间】:2019-04-22 18:08:04
【问题描述】:
我正在研究 Angular 7 中的动态形式。我遵循了 Angular 官方网站上提到的方式。我成功实施了该教程,但我的情况不同。表单中会有什么样的输入,它的所有属性都来自 API 响应
这是带来我的数据的服务功能。 请注意:基于 API 数据,我将填充我的问题数组,现在我使用虚拟数据只是为了确保流程正常工作。这就是我的 question.service.ts 的样子
public response : QuestionBase<any>[] =[];
getQuestions(id) {
this.http.get(this.url+'selectAllFieldDetails/'+id)
.pipe(map(
( response: Response) => {
let data = response;
console.log(data);
let questions: QuestionBase<any>[] = [
new DropdownQuestion({
key: 'brave',
label: 'Bravery Ratings',
class:'form-control fis-form-control',
formclass:'',
options: [
{key: 'solid', value: 'Solid'},
{key: 'great', value: 'Great'},
{key: 'good', value: 'Good'},
{key: 'unproven', value: 'Unproven'}
],
required: true,
order: 2
}),
new TextboxQuestion({
key: 'process',
label: 'Process name',
value: 'IT ',
class:'form-control fis-form-control',
formclass:'',
required: true,
order: 1
}),
];
this.response = questions.sort((a, b) => a.order - b.order);
}
));
console.log(this.response);
return this.response;
}
但问题是函数在 API 响应之前返回并且它返回空数组。这是我从 create-ticket.component.ts 调用的方式
ngOnInit() {
this.questions = this.service.getQuestions(24);
console.log(this.questions);
}
不知何故,我尝试先获取 API 响应并将其存储在某个变量中,然后调用此函数 this.service.getQuestions(24);但随后它显示错误
Error: Cannot find control with name: 'process'
at _throwError (forms.js:1775)
at setUpControl (forms.js:1683)
at FormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective.addControl (forms.js:4532)
at FormControlName.push../node_modules/@angular/forms/fesm5/forms.js.FormControlName._setUpControl (forms.js:5030)
at FormControlName.push../node_modules/@angular/forms/fesm5/forms.js.FormControlName.ngOnChanges (forms.js:4980)
at checkAndUpdateDirectiveInline (core.js:9239)
at checkAndUpdateNodeInline (core.js:10507)
at checkAndUpdateNode (core.js:10469)
at debugCheckAndUpdateNode (core.js:11102)
at debugCheckDirectivesFn (core.js:11062)
Formgorup 数据保持为空。 请帮我解决这个问题。 在此先感谢
【问题讨论】:
-
请提供所有代码,甚至最好提供一个堆栈闪电战,以最大限度地减少故障排除。
-
问题是:您没有订阅获取请求以启动请求。
标签: angular angular7 angular-reactive-forms dynamic-forms form-control