Angular 7 - How to make "Else if" with "*ngif"?

Asked

Viewed 14,955 times

1

I am trying to make an "Else if", to present the items of the category chosen by the user through a radio button. Only I only have "if" and "Else" to present on ng-template.

I set up a sequence of photos to explain the question better:

  1. The screen appears like this first: inserir a descrição da imagem aqui

  2. If you select "Category", a table appears: inserir a descrição da imagem aqui

  3. If you read "Companies", another table appears: inserir a descrição da imagem aqui

the rest of the code works smoothly, only this question of how to do appears the option that the user choose (in the example has 3 options [with the blank screen], if the answer is applied to 3 or more options, will also be accepted).

The html code of the component:

<a (click)="goBack()" class="btn btn-warning btn-md m-3">
  <span class="glyphicon glyphicon-backward"></span><strong> &lt;&lt; Voltar </strong>
</a>
<div class="form-group">
  <label for="changeTipoTemplate" class="mx-3">Selecione o tipo de pesquisa: </label>
  <div class="form-check">
    Categoria<input class="form-check-inline ml-2" type="radio" name="optTipoTabela" value="1" id="checkinfo_1" (change)="changeTipoTemplate(1)">
    Empresa<input class="form-check-inline ml-2" type="radio" name="optTipoTabela" value="0" id="checkinfo_0" (change)="changeTipoTemplate(0)">
  </div>
</div>
<h4 class="m-3">Fiscal</h4>
<ng-container *ngIf="tipo_template === 1; else empresaTemplate">
  <div class="form-group w-50 mx-3">
    <label for="fiscalCategorias">Selecione a categoria:</label>
    <select class="form-control" id="fiscalCategorias" (change)="changeTable()">
      <option value="casn">Código Acesso Simples Nacional</option>
      <option value="municipios">Municípios</option>
      <option value="estados">Estados</option>
      <option value="ibge">IBGE</option>
      <option value="sefaz">SEFAZ</option>
      <option value="nfgaucha">NF Gaúcha</option>
      <option value="parcelamentos">Parcelamentos</option>
      <option value="prefeitura">Prefeitura</option>
      <option value="nfse">NFS-e</option>
    </select>
  </div>

  <div class="row mx-3">
      <prime-table [th]="th" *ngIf="fiscais.length > 0" [td]="fiscais"
          [btn_cadastra]="true" (outputAdd)="cadastrar()"
          [btn_edita]="true" (outputEdita)="editar($event)"
          [btn_deleta]="true" (outputDeleta)="deletar($event)"
      ></prime-table>
  </div>
</ng-container>
<ng-template #empresaTemplate>

  <div class="form-group w-50 mx-3">
    <label for="empresaCategorias">Selecione a empresa:</label>
    <select class="form-control" name="empresaCategorias" id="empresaCategorias" (change)="getAllRegistersOfEmpresa($event)">
      <option value="0"></option>
      <option value="{{ e.nome_empresa }}" *ngFor="let e of empresas">{{ e.nome_empresa }}</option>
    </select>
  </div>

  <div class="row mx-3">
    <prime-table [th]="th" *ngIf="fiscais.length > 0" [td]="fiscais"
        [btn_generico1]="true" [icone_generico1]="'fas fa-edit'" (outputGenerico1)="editarEmpresa($event)"
        [btn_generico2]="true" [icone_generico2]="'fas fa-eye'" (outputGenerico2)="ver($event)"
        [btn_deleta]="true" (outputDeleta)="deletar($event)"
    ></prime-table>
  </div>
</ng-template>
  • 2

    I believe you have the answer for what you need: https://stackoverflow.com/questions/49217822/how-to-render-a-div-on-selection-of-radio-button-angular-2

  • Opa, I took a look and it seemed that in the answer, the result is type "true or false", that ngIf already does by default when, displaying a template or other template. In my case, a template of 3 or 4 would have to appear on the screen

  • 1

    Vc would do <ng-container *ngIf="type_template === 1">, in the other <ng-container *ngIf="type_template === 2">, in another <ng-container *ngIf="type_template === 3"> etc.. From what I understand does not need Else, it will show your container only if the type_template is what you are checking

  • I did a test here, putting values from 0 to 3 on the Uttons radio, and did some ng-template, and apparently it worked. Posted as an answer that I will accept her @Andrevicente

1 answer

2


In case your project will not need to use else, only using the *ngIf in each ng-container you can display the content according to the selected item.

I’ll put the file function . ts for future reference.

changeTipoTemplate(tipo) {
     this.tipo_template = tipo;
}

In your html you pass the value of the selected item.

<div class="form-check">
   Categoria<input class="form-check-inline ml-2" type="radio" name="optTipoTabela" value="1" id="checkinfo_1" (change)="changeTipoTemplate(1)">
   Empresa<input class="form-check-inline ml-2" type="radio" name="optTipoTabela" value="0" id="checkinfo_0" (change)="changeTipoTemplate(0)">
   Outro<input class="form-check-inline ml-2" type="radio" name="optTipoTabela" value="2" id="checkinfo_2" (change)="changeTipoTemplate(2)">
</div>

Now in the same html you assemble the content the way you want to show it to agree with the selected option

<ng-container *ngIf="tipo_template === 0">
     <!-- Conteúdo 0 aqui -->
</ng-container>

<ng-container *ngIf="tipo_template === 1">
     <!-- Conteúdo 1 aqui -->
</ng-container>

<ng-container *ngIf="tipo_template === 2">
     <!-- Conteúdo 2 aqui -->
</ng-container>
  • I think a switch would be better -> https://angular.io/api/common/NgSwitch

Browser other questions tagged

You are not signed in. Login or sign up in order to post.