如何将$ last ngRepeat结果放出href?

时间:2015-07-05 16:15:23

标签: angularjs angularjs-ng-repeat angularjs-ng-switch angularjs-ng-if

有没有选项可以将$ last ng-repeat结果设为<li class="active">Four</li>而不是<li><a href="#">Four</a></li>? 我一直在试用ng-if,但不幸的是它还没有给出好的解决方案:(

DEMO:http://codepen.io/anon/pen/bdvVRe

&#13;
&#13;
var app = angular.module('myapp', []);
app.controller('testController', function($scope) {
  $scope.mymenu = ["one", "two", "three", "four"];
});
&#13;
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
  <div ng-controller="testController" class="container">
    <strong>Expecting</strong>
    <br />
    <ol class="breadcrumb">
      <li><a href="#">One</a></li>
      <li><a href="#">Two</a></li>
      <li><a href="#">Three</a></li>
      <li class="active">Four</li>
    </ol>
    <hr />
    <strong>Current result</strong>
    <ol class="breadcrumb">
      <li ng-repeat="menu in mymenu"><a href="#">{{menu}}</a></li>
    </ol>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:4)

<li ng-repeat="menu in mymenu" ng-class="{active: $last}">
    <a ng-if="!$last" href="#">{{menu}}</a>
    <span ng-if="$last">{{menu}}</span>
</li>

演示:http://codepen.io/anon/pen/domYZL