ng-switch不工作,即使处于活动状态也是错误的

时间:2019-05-03 13:26:52

标签: javascript angularjs ng-switch

ng-switch中有两个按钮,如果isActive为false,则应该显示一个按钮,但是同时显示两个按钮。我在做什么错了?

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


启用                                                                                                                  

3 个答案:

答案 0 :(得分:1)

我建议将ng-if用于布尔值。

ava-spec

但这是ng-switch的示例

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

答案 1 :(得分:1)

角度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> 评估为变量,而不是 false boolean,因此将您的开关大小写更改为value

  

示例

ng-switch-when="'false'"

答案 2 :(得分:1)

您不想在此处使用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变量返回true时,将显示该按钮。返回false时,该按钮将被隐藏。

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

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