0

我在使用 angularjs ng-switch 时遇到问题

JS

function TestCtrl($scope) {
    $scope.currentUser = {"userId":"1","userRole":"N"};
    $scope.userRoles = {"normal":"N","admin":"A"}
    $scope.patient = {name: 'John'};
}

HTML

    <div ng-switch on="currentUser.userRole">
      <a ng-switch-when="userRoles.normal" href="normalUrl"> 
          {{patient.name}} 
      </a>
      <a ng-switch-when="userRoles.admin" href="adminUrl"> 
          {{patient.name}} 
      </a>
      <div ng-switch-default> default </div>
    </div> 

</div>

我希望显示患者姓名并带有指向 normalUrl 的链接,但会显示“默认”。我究竟做错了什么?

这是代码的小提琴

4

1 回答 1

1

ngSwitchWhen指令不评估表达式(尽管我听说这可能会添加到 1.3)。该值被插入为字符串文字,因此您必须像这样使用它:

<a ng-switch-when="N" href="normalUrl">

这会起作用,但如果你真的需要动态确定你的 when 价值,那么也许ngIf会更好地满足你的需求:

<a ng-if="currentUser.userRole === userRoles.normal" href="normalUrl">

<a ng-if="currentUser.userRole === userRoles.admin" href="adminUrl">
于 2014-04-08T17:24:26.387 回答