Javascript 在angularjs中使用ng repeat列出树结构时出错
我正在尝试使用ng repeat在html上列出一个嵌套字典 这是全文 但我似乎无法通过访问dictionary key的值Javascript 在angularjs中使用ng repeat列出树结构时出错,javascript,html,angularjs,tree,angularjs-ng-repeat,Javascript,Html,Angularjs,Tree,Angularjs Ng Repeat,我正在尝试使用ng repeat在html上列出一个嵌套字典 这是全文 但我似乎无法通过访问dictionary key的值 tree.value 因为它要么不显示,要么只打印 'tree.value' 有人能帮我吗 <div ng-app> <script type="text/ng-template" id="myTemplate"> <li>X</li> <p>1 tree.value</p&
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>
X
1.价值
2.价值
似乎c1是对象而不是字符串。因此c1具有属性。例如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
中定义树
变量,而不是在html上
你知道为什么
{{tree.value}}
必须在ul
标记之外而不是ul
标记之内吗?@Kevin因为当
标记没有树。子项则ul
将不会通过ng repeat
呈现,这就是为什么它应该位于ul