如何通过angularJS中的嵌套键值对正确地重复ng-repeat

时间:2021-05-13 07:36:47

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:

JS:

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

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

HTML:

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>

3 个解决方案

#1


22  

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

你非常接近,我更新了小提琴。 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


0  

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


0  

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>

#1


22  

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

你非常接近,我更新了小提琴。 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


0  

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


0  

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>