AngularJs:ng-repeat =“arrayValue中的项目”

时间:2015-02-21 19:22:03

标签: javascript angularjs

我有以下数组:

 var date = {
   '2015': [{
     'month': 'Januar',
     'day': 31
   }, {
     'month': 'Februar',
     'day': 28
   }]
 };

我想让ng-repeat等于天数:
需要输出:

<div ng-repeat="date in date.2015">
  {{date.month}}
  <!-- Displays 12 month name -->
  <div ng-repeat="day in date.2015+{{$index}}+'.day'">
    {{day}}
    <!-- displays 31, 30 or 28 times the day -->
    <!-- The day should also increment from 0 to the number -->
  </div>
</div>

我希望这很清楚 ng-repeat="day in date.2015+{{$index}}+'.day'"不起作用

2 个答案:

答案 0 :(得分:3)

首先,您需要在控制器中使用辅助功能,将您的号码转换为长度等于当天值的数组:

$scope.range = function(n) {
    return new Array(n);   
}

如果您使用的是最新版本的angular,则需要在repeat to alias中明确设置变量。 Working fiddle with the code you provided

<div ng-repeat="date in date.2015">
   {{date.month}} <!-- Displays 12 month name -->
   <div ng-repeat="day in range(date.day) track by $index">
        {{$index + 1}} <!-- displays 31, 30 or 28 times the day -->
        <!-- The day should also increment from 0 to the number -->
   </div>
</div>

然后你需要调整你的第二个ng-repeat以利用控制器的功能来获取ng-repeat的索引并添加1,因为它基于0。 Working Plunker with old version

<div ng-repeat="date in date.2015">
   {{date.month}} <!-- Displays 12 month name -->
   <div ng-repeat="day in range(date.day)">
        {{$index + 1}} <!-- displays 31, 30 or 28 times the day -->
        <!-- The day should also increment from 0 to the number -->
   </div>
</div>

答案 1 :(得分:0)

我希望我能做到你想做的事。要重复这些日子,请执行以下操作:

<div ng-repeat="date in date.2015">
   {{date.month}} <!-- Displays 12 month name -->
   <div ng-repeat="day in getDays(date.day)">
        {{day}} <!-- displays 31, 30 or 28 times the day -->
        <!-- The day should also increment from 0 to the number -->
   </div>
</div>

现在,只需在控制器中添加此功能:

$scope.getDays = function(num) {
    return new Array(num);   
}