ng-switch 不工作,即使 isactive 是 false

ng-switch not working even isactive is false

ng-switch 中有两个按钮,如果 isActive 为 false,应该显示其中一个,但它同时显示 buttons.what 我做错了吗?

<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>


启用

我建议对布尔值使用 ng-if。

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

但这是 ng-switch 的示例

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

Angular ng-switch 会将你的 false 评估为变量而不是 boolean value 所以改变你的开关盒作为 ng-switch-when="'false'"

example

<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>
                                                    <div ng-switch-default>                                                                
                                                        <button type="button" (click)="activateDeactivateUser(user.UserId,user.IsActive)" class="btn btn-primary active">Activate</button>
                                                    </div>
                                            </div>

你不想在这里使用 ngSwitch,而是 ngIf:

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

当user.IsActive变量returns为真时,按钮会显示。当它 returns 为 false 时,按钮将被隐藏。

另外,您对 ngSwitch 的语法不正确。正确的语法遵循以下模式:

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