0

I have posted my question How to create check box from multidimensional array using angular js

But I have not gotten any response. I have debug my code when I am going to add second ng-repeat I am getting

Error: ngRepeat:dupes Duplicate Key in Repeater

When I am adding repeat by $index I am getting single character as key and value too

Please help me I am new in angular js :(

This is my fiddle of code http://jsfiddle.net/ashishoft/03L3faq5/2/

6
  • Can you add some code to this question. It's difficult to understand what you are trying to achieve. Commented May 5, 2016 at 17:32
  • I looked at your other question, and both of your questions don't make it very clear what you are asking. Commented May 5, 2016 at 17:32
  • Rather than post a new question, it'd be better if you edited the existing question. Adding a Codepen or plunker would help a lot. Commented May 5, 2016 at 17:33
  • 1
    The correct syntax is ng-repeat="value in list track by $index" Docs: docs.angularjs.org/error/ngRepeat/dupes Commented May 5, 2016 at 17:33
  • sounds like you have the same key inside of two nested loops? something like ng-repeat="foo in list" and within that, ng-repeat="foo in list2". change one of the foo's to something else. Commented May 5, 2016 at 17:40

3 Answers 3

1

Here is example:

ng-repeat="i in items track by $index"
Sign up to request clarification or add additional context in comments.

Comments

0

Pretty simple really. You can even use it in combination with custom directives etc. Its basically an index of where you are in the loop.

There are some flaws with using it though. If your creating an ng-repeat list, and then passing the $index into some click function or something, then you can have issues if your also using filtering on that same list. The $index can fall out of sync with the filters and leave you wondering why its giving you wrong data.

for example:

<li ng-repeat="page in links | someFilter:filter">
    <a ng-click="someFunction($index)">{{ someData }}</a>
</li>

vs the better way of:

<li ng-repeat="itemX in someData | someFilter:filter">
    <a ng-click="someFunction(itemX)">{{ itemX.name }}</a>
</li>

So although I use the $index in my example below, its often best just to pass the whole object in when doing more complex functions per row.

HTML:

<div ng-app="myApp" ng-controller="MainCtrl">
    <ul>
        <li ng-repeat="page in links">
            <a ng-class="{ testClass: $index == pageNumber }" ng-click="setPageNumber($index)">{{ page }}</a>
        </li>
    </ul>
</div>

Javascript:

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

app.controller('MainCtrl', function($scope) {
    $scope.links = [
        'Link One',
        'Link Two',
        'Link Three'
    ];

    $scope.pageNumber = 0;
    $scope.setPageNumber = function(index) {
        $scope.pageNumber = index;
    }
});

CSS:

.testClass {
    background: black;
    color: white;
}

Comments

0

This is a sample of how it could be written, although using $index is just fine. Might I suggest using something else like the element id or title, here is an article that talks about ng-repeat.

<div class="favorite-children" ng-repeat="photo in ctrl.favorties track by $index">
    <div class="card"></div>
</div>

  //or

<div class="favorite-children" ng-repeat="photo in ctrl.favorties track by photo.title">
  <div class="card"></div>
</div>

Update

Try this FIDDLE for a preview of a comprehensive JSON, with your same code previewed on how you can use it. Hope it helps.

4 Comments

0 : [ 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 : 10 : 11 : 12 : 13 : 14 : 15 : 16 : 17 : 18 : { 19 : 20 : 21 : 22 : 23 : 24 : 25 : 26 : 27 : 28 : 29 : 30 : 31 : 32 : 33 : 34 : 35 : 36 : 37 : 38 : 39 : 40 : " 41 : t 42 : a 43 : g 44 : " 45 : : 46 : 47 : " I have added ng-repeat="(key, value) in itemse.dietry_reqs track by $index" in side the first ng-repeat I am getting key and value data as like above :(
I have added my fiddle above in question.
@mishraoft is the only way the json comes in? I would recommend in the long run maybe refactoring the format. I will include a new forked version so you can see it in action with a large json.
@mishraoft also, what version of angular are you using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.