1

I have a javascript array like the following:

var array = [
   2005 = [
       Jan = [0,1,2,3,5...],
       Feb = [0,1,2,3,5...],
       ...more
   ], 
   2006 = [
       Jan = [0,1,2,3,5...],
       Feb = [0,1,2,3,5...],
       ...more
   ], 
   2007 = [
       Jan = [0,1,2,3,5...],
       Feb = [0,1,2,3,5...],
       ...more
   ]

]

$scope.cal = array;

I am trying to build a calendar with ng-repeat

<div ng-repeat = "year in cal">
    {{year}}
</div>

There is nothing output and I got an error

Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. 
Repeater: year in cal, Duplicate key: undefined:undefined, Duplicate value: undefined

I am not sure what I did wrong and my brain is fried. Please help me out on this one. Thanks!

6
  • 1
    <div ng-repeat = "year in cal track by $index"> Commented Oct 30, 2014 at 6:05
  • @KalhanoToressPamuditha I did try that, all I got is 3 lines of [] Commented Oct 30, 2014 at 6:11
  • @KalhanoToressPamuditha not sure what your question mean. I got [] [] [] for three different divs Commented Oct 30, 2014 at 6:13
  • 1
    your array is not valid i think Commented Oct 30, 2014 at 6:18
  • when I console.log(array) it displays fine. Commented Oct 30, 2014 at 6:18

3 Answers 3

1

You need a common property between the different year entries that you can bind to. So you need some kind of a value property which holds the identifier like the year or the month and another property that holds the value array. See the example below.

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function ($scope) {
    $scope.cal = [
        {
            value: 2005,
            month:
            [
                {
                    value: 'Jan',
                    days: [0, 1, 2, 3]
                },
                {
                    value: 'Feb',
                    days: [0, 1, 2, 3]
                }
            ]
        },
        {
            value: 2006,
            month:
            [
                {
                    value: 'Jan',
                    days: [0, 1, 3, 7]
                },
                {
                    value: 'Feb',
                    days: [0, 1, 2, 3]
                }
            ]
        }
    ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
    <div ng-controller="MyCtrl">
      <div ng-repeat = "year in cal">
          Year: {{year.value}}
          <div ng-repeat = "month in year.month">
              Month: {{month.value}}
              <div ng-repeat = "day in month.days">
                Day: {{day}}
              </div>
          </div>
      </div>
    </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

Check the working plunker here

Apart from the syntax problems of JSON object the design should be something like this:

    [
        {
           yearNum: 2005,
           months: [
                        {Jan : [0,1,2,3,5]},
                        {Feb : [0,1,2,3,5]},
                      ]
        }, 
        {
           yearNum: 2006,
           months: [
                        {Jan : [0,1,2,3,5]},
                        {Feb : [0,1,2,3,5]},
                      ]
        }
    ]

1 Comment

Thanks! but Denis's is better for what I need! +1
0

Try formatting you array like this.

var array = [
   { 2005: [
      { Jan: [0, 1, 2, 3, 5] },
      { Feb: [0, 1, 2, 3, 5] },
   ]
}];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.