简体   繁体   English

在angularjs中使用ng-repeat列出树结构时出错

[英]Error in listing tree structure using ng-repeat in angularjs

I am trying to list a nested dictionary on html using ng-repeat 我正在尝试使用ng-repeat在html上列出嵌套的字典

Here is the full code 这是完整的代码

But it seems like I can't access the value of dictionary key by 但是似乎我无法通过以下方式访问字典键的值

tree.value

as it either doesn't show up or will only print 因为它要么不显示,要么仅打印

'tree.value'

Can someone help me out? 有人可以帮我吗?

<div ng-app>
  <script type="text/ng-template" id="myTemplate">
      <li>X</li>
      <p>1 tree.value</p>
      <ul ng-repeat="tree in tree.children" ng-include="'myTemplate'">
          <p>2 tree.value</p>
      </ul>
  </script>
 <div>
     <ul ng-include="'myTemplate'"
    ng-init="tree = 
               {
        children: 
            [
                {children: [], value: c12}, 
                {
                    children: 
                        [
                            {children: [], value: c112}, 
                            {children: [], value: c111}
                        ],
                    value: c11
                }
            ], 
        value: c1
    }
"></ul>
  </div>
</div>

You json is wrongly defined, you should always wrap string value inside single quotes ' like value: c111 & value: 'c112' values should be like value: 'c111' & value: 'c112' 如果您定义的json错误,则应始终将字符串值包装在单引号中'value: c111value: 'c112'值应该像value: 'c111'value: 'c112'

Markup 标记

    <ul ng-include="'myTemplate'"
        ng-init="tree = 
                   {
            children: 
                [
                    {children: [], value: 'c12'}, 
                    {
                        children: 
                            [
                                {children: [], value: 'c112'},
                                {children: [], value: 'c111'}
                            ],
                        value: c11
                    }
                ], 
            value: c1
        }
"></ul>

More better would be define tree variable inside $scope rather than on html. 更好的是在$scope而不是在html上定义tree变量。

Working JSFiddle 工作中的JSFiddle

it seems c1 is object not string.so c1 has properties.for example c1 has text property. 似乎c1不是对象,所以c1具有属性。例如c1具有text属性。

you should write in template {{tree.value.text}} 您应该使用模板{{tree.value.text}}编写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM