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

Error in listing tree structure using ng-repeat in angularjs

我正在尝试使用 ng-repeat

在 html 上列出嵌套字典

这里是完整的code

但我似乎无法通过

访问字典键的值
tree.value

因为它要么不显示,要么只打印

'tree.value'

有人能帮帮我吗?

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

似乎 c1 不是对象 string.so c1 有 properties.for 示例 c1 有文本 属性.

你应该在模板中写{{tree.value.text}}

您 json 的定义有误,您应该始终将字符串值用单引号括起来 ' 就像 value: c111 & value: 'c112' 值应该像 value: 'c111' &value: 'c112'

标记

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

$scope 内定义 tree 变量比在 html.

上定义更好

Working JSFiddle