如何在Angular 7中创建动态表单?

时间:2019-05-23 13:55:00

标签: angular forms typescript formgroups

我有一个注册“登录名”的表单,但是每个登录名是不同的,一个有3个字段,其他4个...因此,我的表单必须是动态的。我使用formGroups和验证器。

我有17种“登录”类型,其字段如下:

我尝试在字段中插入Validator,但是只有其中一些必须具有Validators,只有“登录”表单的字段。

this.fiscalForm = this.formBuilder.group({
      id_fiscal: [null],
      codigo_empresa: [null],
      nome_empresa: [null],
      cnpj: [null],
      cpf: [null],
      ie: [null],
      im: [null],
      socio_responsavel: [null],
      contador: [null],
      municipio: [null],
      uf: [null],
      login: [null],
      senha: [null],
      site: [null],
      observacao: [null],
      aliquota: [null],
      conta: [null],
      codigo_acesso: [null],
      inscricao: [null],
      total_par_valor: [null],
      tipo_tabela: [null, Validators.required],
    });

private setCurrentAction() {
    if(this.route.snapshot.url[0].path == 'new') {
      this.currentAction = 'new';
      this.tipo_tabela = this.route.snapshot.url[1].path;

      if(this.route.snapshot.url[1].path == 'municipios'){
        this.pageTitle = 'Criar cadastro (Municípios)';

      } else if(this.route.snapshot.url[1].path == 'estados'){
        this.pageTitle = 'Criar cadastro (Estados)';

      } else if(this.route.snapshot.url[1].path == 'casn'){
        this.pageTitle = 'Criar cadastro (Código Acesso Simples Nacional)';

      } else if(this.route.snapshot.url[1].path == 'ibge'){
        this.pageTitle = 'Criar cadastro (IBGE)';

      } else if(this.route.snapshot.url[1].path == 'nfgaucha'){
        this.pageTitle = 'Criar cadastro (NF Gaúcha)';

      } else if(this.route.snapshot.url[1].path == 'sefaz'){
        this.pageTitle = 'Criar cadastro (SEFAZ)';

      } else if(this.route.snapshot.url[1].path == 'parcelamentos'){
        this.pageTitle = 'Criar cadastro (Parcelamentos)';

      } else if(this.route.snapshot.url[1].path == 'prefeitura'){
        this.pageTitle = 'Criar cadastro (Prefeitura)';

      } else if(this.route.snapshot.url[1].path == 'nfse'){
        this.pageTitle = 'Criar cadastro (NFS-e)';

      } else if(this.route.snapshot.url[2].path == 'refis i'){
        this.pageTitle = 'Criar cadastro - Refis I (Parcelamentos)';

      } else if(this.route.snapshot.url[2].path == 'refis ii paes'){
        this.pageTitle = 'Criar cadastro - Refis II Paes (Parcelamentos)';

      } else if(this.route.snapshot.url[2].path == 'refis iv'){
        this.pageTitle = 'Criar cadastro - Refis IV (Parcelamentos)';

      } else if(this.route.snapshot.url[2].path == 'simples 2007'){
        this.pageTitle = 'Criar cadastro - Simples 2007 (Parcelamentos)';

      } else if(this.route.snapshot.url[2].path == 'pgfn'){
        this.pageTitle = 'Criar cadastro - PGFN (Parcelamentos)';

      } else if(this.route.snapshot.url[2].path == 'issqn'){
        this.pageTitle = 'Criar cadastro - ISSQN (Parcelamentos)';

      } else if(this.route.snapshot.url[2].path == 'icms estoque'){
        this.pageTitle = 'Criar cadastro - ICMS Estoque (Parcelamentos)';
        ;

      } else if(this.route.snapshot.url[2].path == 'icms'){
        this.pageTitle = 'Criar cadastro - ICMS (Parcelamentos)';

      } else {
        this.pageTitle = 'Page Not Found 404';
      }
    }

如何仅显示“登录”表单中的字段并将验证器放在该字段中?

0 个答案:

没有答案