Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to use ng-repeat again and again?

I am building one application in AngularJS and I have very complex JSON file which have so many child arrays and objects. So my question is: for accessing the data from JSON is it OK to use ng-repeat again and again?

<div ng-repeat="parent in parents">
      <div ng-repeat="child in parent">
           <div ng-repeat="grandChild in child">
                {{grandChild.name}}
           </div>
      </div>
 </div>

----- OR there is some looping method is available in AngularJS

----- OR we have to use old JavaScript for loop

Sample data

{"data":
    {
        "categories":
        {
            "articles":
            {
                "bdh":
                [
                    {"id":1, "name":"bdh article 1", "body":"this is bdh article 1 body."},
                    {"id":2, "name":"bdh article 2", "body":"this is bdh article 2 body."}
                ],
                "hadoop":
                [
                    {"id":3, "name":"hadoop article 1", "body":"this is hadoop article 1 body."},
                    {"id":4, "name":"hadoop article 2", "body":"this is hadoop article 2 body."}
                ]
            },
            "videos":
            {
                "bdh Videos":
                [
                    {"id":5, "name":"bdh videos 1", "body":"this is bdh videos 1 body."},
                    {"id":6, "name":"bdh videos 2", "body":"this is bdh videos 2 body."}
                ],
                "hadoop Videos":
                [
                    {"id":7, "name":"hadoop videos 1", "body":"this is hadoop videos 1 body."},
                    {"id":8, "name":"hadoop videos 2", "body":"this is hadoop videos 2 body."}
                ]
            }   
        }
    }
}
like image 816
Akshay Kumar Avatar asked Jan 30 '16 12:01

Akshay Kumar


People also ask

What does ng-repeat mean?

Definition and Usage The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

What is ng-repeat end?

The ng-repeat-start directive works the same as ng-repeat, but will repeat all the HTML code (including the tag it's defined on) up to and including the ending HTML tag where ng-repeat-end is placed.


1 Answers

If you have a deeply nested structure, and you want to access every item within it, then yes, nesting ng-repeats is perfectly fine. It’s just like nesting for loops inside another to access all levels of an object.

Of course, if your object is very big, then you should consider not displaying everything, but that applies to all large objects regardless of whether they have the information nested or in a flat structure.

like image 182
poke Avatar answered Oct 05 '22 11:10

poke