Angular 2 - ngSwitchCase

时间:2017-08-01 13:19:35

标签: angular typescript switch-statement

我正在使用Angular 2学习Switch Case。我试图回显数组,但我必须重命名一些单词。这就是我之前所说的:

<tbody *ngFor="let access of menu.navAccess" class="tr-content">
      <tr class="trParent">
          <td>{{ access.access | uppercase }}</td>
          <td>{{ access.accessType }}</td>
          <td><span class="glyphicon glyphicon-trash actionsBtn" data-toggle="modal" data-target="#deleteModal" (click)="showDelete(access)"></span></td>
      </tr>
</tbody>

现在,我想在ngSwitchCase上添加access.accessType

<tbody *ngFor="let access of menu.navAccess" class="tr-content">
       <tr class="trParent">
           <td>{{ access.access | uppercase }}</td>
           <td [ngSwitch]="access.accessType">
               <span *ngSwitchCase="BusinessUnit">Business Unit</span>
           </td>
           <td><span class="glyphicon glyphicon-trash actionsBtn" data-toggle="modal" data-target="#deleteModal" (click)="showDelete(access)"></span></td>
       </tr>
 </tbody>

所以,我知道我的access.accessType等于BusinessUnit,但在我的界面中,我想展示Business Unit,但没有显示任何内容。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

好的,我找到了解决方案。因为* ngSwitchCase想要一个表达式,我必须将我的SwitchCase声明为一个字符串,如下所示:

<span *ngSwitchCase="'BusinessUnit'">Business Unit</span>

用单引号代替:

<span *ngSwitchCase="BusinessUnit">Business Unit</span>