javascriptangularjsng-switch

ng-switch not working even isactive is false


There are two buttons in ng-switch, if isActive is false, either one should show, but it is showing both buttons.what wrong i am doing?

<div ng-switch="user.IsActive">
  <div ng-switch-when="false">
  <button type="button" (click)="activateDeactivateUser(user.UserId,user.IsActive)" class="btn btn-primary active">Deactivate</button>
    </div>


Activate


Solution

  • You don't want to be using an ngSwitch here, but rather an ngIf:

    <div *ngIf="user.IsActive">
      <button type="button" (click)="activateDeactivateUser(user.UserId,user.IsActive)" class="btn btn-primary active">Deactivate</button>
    </div>
    

    When the user.IsActive variable returns true, the button will display. When it returns false, the button will be hidden.

    On a separate note, your syntax for the ngSwitch is incorrect. The proper syntax follows this pattern:

    <div [ngSwitch]="variableCondition">
        <component1 *ngSwitchCase="variableCondition1"></component1>
        <component2 *ngSwitchCase="variableCondition2"></component2>
    </div>