如何将url中的变量传递给ng-repeat?

时间:2014-11-24 21:41:33

标签: arrays angularjs ng-repeat

如何在网址中使用ng-repeat和变量?我的网址是这样的:#/timeline/{{year}}/{{month}}

<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
       <li ng-repeat="months in dateMonths"><a href="#/timeline/{{months}}">{{months}}</a></li>
</ul>

范围内的数组:

$scope.dateMonths = {2014: ['jun', 'jul', 'aug', 'sep', 'nov', 'dec']}, {2013: ['jan', 'feb', 'apr', 'oct']};

如果我把年份放在名为函数的数组中

<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
       <li ng-repeat="months in dateMonths.2014"><a href="#/timeline/{{months}}">{{months}}</a></li>
</ul>

但我想要一些东西,例如:

<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
       <li ng-repeat="months in dateMonths.{{year}}"><a href="#/timeline/{{months}}">{{months}}</a></li>
</ul>

怎么办?

1 个答案:

答案 0 :(得分:0)

您可以为其中一个对象执行此操作:

<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
   <div ng-repeat="(year, months) in dateMonths">
   <li ng-repeat="month in months"><a href="#/timeline/{{month}}">{{month}}</a></li>
   </div>
</ul>

但是,如果要对所有对象进行ng重复,则需要为每个对象提供变量,或者需要创建对象数组。如果您决定创建一个对象数组,则可以执行以下操作:

<ul role="menu" aria-labelledby="dLabel">
   <div ng-repeat="(object, year) in dateMonths">
      <div ng-repeat="(year, months) in year">
      {{year}}
       <li ng-repeat="month in months"><a href="#/timeline/{{month}}">{{month}}</a></li>
      </div>
   </div>
</ul>

,其中

  $scope.dateMonths = [{2014: ['jun', 'jul', 'aug', 'sep', 'nov', 'dec']}, {2013: ['jan', 'feb', 'apr', 'oct']}];