Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly ng-repeat through nested key value pairs in angularJS

View live code:

Angular JS

How in the world do I properly loop through nested key value pairs and properly output them like below?

View I want is a tree like so

-touts
  -classes
    -col-12 
    -col-md-12
    -col-lg-12

Currently the view is:

touts
  {"classes":["col-12","col-md-12","col-lg-12"]}

JS:

var currentApp = angular.module('currentApp', []);
currentApp.controller('ACtrl', function($scope){

    $scope.templates = {
        'touts' : [
            {
                'classes' : ['col-12', 'col-md-12', 'col-lg-12' ]
            }
        ]
    };
});

HTML:

<div ng-app="currentApp">
    <div ng-controller="ACtrl">
        <ul ng-repeat="(key, prop) in templates">
            <li>{{key}}</li>
              <li>
                  <ul ng-repeat="class in templates[key]">
                      <li>{{class}}</li>
                  </ul>
            </li>
        </ul>
    </div>
</div>
like image 531
Armeen Harwood Avatar asked Jan 30 '14 01:01

Armeen Harwood


People also ask

How do you use ng repeat in HTML?

Following is the general syntax of using the ng-repeat directive: Where, the div can be replaced by any elements like h1 to h6 headings, span, paragraph tags etc. … The repeat expression in the syntax can be used in different ways, for example: See the following examples for learning more about the ng-repeat angular directive.

How to use nested ng-repeat directive in AngularJS?

The ng-repeat directive instantiates a template once per item from a collection like array, list etc. In this blog, I am using nested ng-repeat to show the country list along with their city name. Create a project and install AngularJS from NuGet Package, add a script file to the project and write Angular code.

How to ask for a key-value pair parameter from AngularJS?

This can be done using parenthesis in the ng-repeat directive to explicitly ask for a key-value pair parameter from angularJS. Here the variable key contains the key of the object and value contains the value of the object. <element ng-repeat=" (key, value) in JSObject"> Contents... </element>

What is complex nested json objects in angular?

Complex nested JSON objects comprises of a JSON object array and each object of the JSON array consisting of another child JSON object array. In such scenario, one has to make use of nested ng-repeat directive i.e. one ng-repeat directive inside another ng-repeat directive, thus it makes nested loops.


1 Answers

You are pretty close, I updated the fiddle. http://jsfiddle.net/y9wj6/9/

   <ul ng-repeat="(key, prop) in templates">
        <li>{{key}}</li>
        <ul ng-repeat="val in prop">
            <ul ng-repeat="(o, values) in val">
            <li>{{o}}</li>
                 <ul ng-repeat="i in values">
                      <li>{{i}}</li>
                  </ul
             </ul>
        </ul>
    </ul>
like image 120
hassassin Avatar answered Oct 27 '22 04:10

hassassin