2

What i need

        if(field.ajax && field.ajax='Y' && field.multiple&& field.multiple=='Y')
        {

        }
        else if (field.ajax && field.ajax='Y' && field.multiple&& field.multiple=='N'))
        {
        }
        else
        {
        }

for 2 conditon works

    if(cond)
        {
        code
        }else
        {
        code
        }

this case code works

   <ng-container *ngIf="field.ajax && field.ajax === 'Y'; else select2ElseBlock">
              <ng-select
              [items]="data"
              multiple="true"
              bindLabel="name"
              [closeOnSelect]="true"
              [loading]="loading"

              [searchable]="true"
              [clearable]="true" 
              (click)="clearModel()"
              (keyup)="changed($event.target.value)">
              </ng-select>
        </ng-container>
       <ng-template #select2ElseBlock>
              <ng-select
              [items]="field.choices.choice"
              multiple="true"
              bindLabel="name"
              [loading]="loading"
              >
              </ng-select>
        </ng-template>

Multiple case i tried

but this case create problem

 <ng-container *ngIf="field.ajax && field.ajax === 'Y'  then  select2thenBlock else select2ElseBlock">
          <ng-select
          [items]="data"
          multiple="true"
          bindLabel="name"
          [closeOnSelect]="true"
          [loading]="loading"

          [searchable]="true"
          [clearable]="true" 
          (click)="clearModel()"
          (keyup)="changed($event.target.value)">
          </ng-select>
    </ng-container>
     <ng-template #select2thenBlock>
          <ng-select
          [items]="field.choices.choice"
          multiple="true"
          bindLabel="name"
          [loading]="loading"
          >
          </ng-select>
    </ng-template>
   <ng-template #select2ElseBlock>
          <ng-select
          [items]="field.choices.choice"
          multiple="true"
          bindLabel="name"
          [loading]="loading"
          >
          </ng-select>
    </ng-template>

But for multiple case create problem.

  • how can implement nested if else conditions in Angular 8.

Refrence

https://angular.io/api/common/NgIf

2
  • And what's the problem? Commented Aug 16, 2019 at 7:38
  • update question problem in multiple conditions. where i need check conditions as well Commented Aug 16, 2019 at 7:48

2 Answers 2

5

You just logically nest the conditions:

<ng-container *ngIf="field?.ajax === 'Y'; else ajaxNotY">
  <ng-container *ngIf="field?.multiple === 'Y'; else multipleNotY">
    AJAX AND MULTIPLE Y
  </ng-container>
  <ng-template #multipleNotY>MULTIPLE NOT Y</ng-template>
</ng-container>
<ng-template #ajaxNotY>AJAX NOT Y</ng-template>
Sign up to request clarification or add additional context in comments.

1 Comment

np, you can also move the #multipleNotY template outside the ng-container if you find that more readable
2

The easiest thing to do might be to take your logic and put it in a function in the component, and then reference that in the ngIfs.

eg.

public showHideFunction() : number {
    if(field.ajax && field.ajax='Y' && field.ajax && field.multiple=='Y')
    {
        return 1;
    }
    else if (field.ajax && field.ajax='Y' && field.ajax && field.multiple=='N'))
    {
        return 2;
    }
    else
    {
        return 3;
    }
}

<div *ngIf="showHideFunction() === 1"></div>
<div *ngIf="showHideFunction() === 2"></div>
<div *ngIf="showHideFunction() === 3"></div>


A bit messy, but it would work.

1 Comment

function calls in template are extremely low performance and generally bad practice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.