javascript 我如何通过 angularJS 中的嵌套键值对正确地重复

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21446253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:55:40  来源:igfitidea点击:

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

javascriptjsonangularjsangularjs-ng-repeat

提问by Matthew Harwood

View live code:

查看实时代码:

Angular JS

角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>

回答by hassassin

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>

回答by Chen-Tsu Lin

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>

回答by Bogus Hawtsauce

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>