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

末鹿安然 提交于 2019-12-29 07:35:16

问题


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>

回答1:


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>



回答2:


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>



回答3:


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>


来源:https://stackoverflow.com/questions/21446253/how-do-i-properly-ng-repeat-through-nested-key-value-pairs-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!