Forms 带有远程元数据的Angular2动态表单
我按照中的说明创建了一个动态表单,然后尝试使用数据库中的元数据创建表单 我向get字段类型、名称、id等发出了一个HTTP请求。但是,当我尝试像示例中那样构建表单时,什么都没有发生,或者控制台上出现错误 以下是教程中的代码:Forms 带有远程元数据的Angular2动态表单,forms,angular,dynamic,metadata,angular2-forms,Forms,Angular,Dynamic,Metadata,Angular2 Forms,我按照中的说明创建了一个动态表单,然后尝试使用数据库中的元数据创建表单 我向get字段类型、名称、id等发出了一个HTTP请求。但是,当我尝试像示例中那样构建表单时,什么都没有发生,或者控制台上出现错误 以下是教程中的代码: export class AppComponent { questions: any[]; constructor(service: QuestionService) { this.questions = service.getQue
export class AppComponent {
questions: any[];
constructor(service: QuestionService) {
this.questions = service.getQuestions();
}
}
这就是我所做的:
export class AppComponent implements OnInit {
campos: any[] = [];
constructor(private servico: FormDadosService) {}
ngOnInit() {
this.servico.getCampos().subscribe(this.processaCampos);
}
processaCampos(dados) {
for (let i = 0; i < dados.length; i++) {
this.campos.push(new CampoBase({
nome: dados[i].ZI2_NOME,
label: dados[i].ZI2_DESC,
ordem: dados[i].ZI2_ORDEM,
obrigatorio: dados[i].ZI2_OBRIGAT,
tamanho: dados[i].ZI2_TAM,
valor: '',
tipoCampo: dados[i].ZI2_TIPO
}))
}
}
}
导出类AppComponent实现OnInit{
坎波斯:任何[]=[];
构造函数(私有服务:FormDadosService){}
恩戈尼尼特(){
this.servico.getCampos().subscribe(this.processaCampos);
}
processaCampos(护墙板){
for(设i=0;i
我得到这个错误:
error\u handler.js:50异常:无法读取未定义的属性“push”
我想我需要知道一种方法,在所有关于表单的数据都从我的HTTP请求中到达之后,如何呈现表单。我是这样做的:
export class AppComponent implements OnInit {
campos: any[] = [];
constructor(private servico: FormDadosService) { }
ngOnInit() {
this.servico.getCampos().subscribe((data) => {
data.forEach(campo => {
this.campos.push(new CampoBase({
valor: '',
nome: campo.ZI2_CAMPO,
label: campo.ZI2_DESC,
tipoCampo: campo.ZI2_TIPO,
tamanho: campo.ZI2_TAM
}))
});
});
}
}
这个问题可以标记为已解决
谢谢大家。您会遇到哪些错误?你能包含代码的相关部分吗?谷歌做到了:导出类AppComponent{questions:any[];构造函数(service:QuestionService){this.questions=service.getQuestions();}}}而我做到了:导出类AppComponent实现OnInit{campos:any[]=[];构造函数(private-servico:FormDadosService){}ngonit(){this.servico.getCampos().subscribe(this.processaCampos);}processaCampos(dados){for(let i=0;i