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

后端 未结 3 1274
独厮守ぢ
独厮守ぢ 2020-12-19 04:25

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

相关标签:
3条回答
  • 2020-12-19 04:45

    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>
    
    0 讨论(0)
  • 2020-12-19 05:02

    So this is pretty old question, however if you modified the object a bit (swap the array types for objects) the following template should work just fine.

    angular.module('init', [])
      .controller('test', ['$scope', function($scope) {
    
        $scope.templates = {
          'touts': {
            'classes': {
              'col-12': {},
              'col-md-12': {},
              'col-lg-12': {}
            }
          }
        };
      }]);
    ul {
      list-style: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    
    <div ng-app="init" ng-controller="test">
    
      <script type="text/ng-template" id="recursive_item.html">
        {{key}}
        <ul>
          <li ng-repeat="(key, prop) in prop" ng-include="'recursive_item.html'"></li>
        </ul>
      </script>
    
      <ul>
        <li ng-repeat="(key, prop) in templates" ng-include="'recursive_item.html'"></li>
      </ul>
    
    </div>

    0 讨论(0)
  • 2020-12-19 05:06

    You must think gradually.

     templates = {'touts' : [{'classes' : ['col-12', 'col-md-12', 'col-lg-12' ] }]};  
     // key = 'touts'
     // props = [{'classes' : ['col-12', 'col-md-12', 'col-lg-12' ] }] 
     // props[0] = {'classes' : ['col-12', 'col-md-12', 'col-lg-12' ] }
     // classkey = 'classes'
     // classprop = ['col-12', 'col-md-12', 'col-lg-12' ]
     // and print classprop by ng-repeat 
    

    So you can try this:

     <div ng-app="currentApp">
        <div ng-controller="ACtrl">
            <ul ng-repeat="(key, props) in templates">
                <li>{{key}}</li>
                <li>
                   <ul ng-repeat="(classkey, classprop) in props">
                      <li>{{classkey}}</li>
                      <li>
                          <ul>
                              <li ng-repeat="class in classprop">
                          </ul>
                      </li>
                   </ul>
                </li>
             </ul>
        </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题