我在我的控制器中声明如下数据结构:
I have the following data structure declared in my controller:
$scope.tree = {
label:"A",
parent:{
label:"B",
parent:{
label:"C"
}
}
};
我愿与落得是:
<ul>
<li>C
<ul>
<li>B
<ul>
<li>A</li>
</ul>
</li>
</ul>
</li>
<ul>
我试着做各种方式这包括外部模板,自定义指令等,我只是似乎无法得到它的工作。
I've tried various ways of doing this including external templates, custom directives etc and I just can't seem to get it to work.
有什么想法?
在你链接到评论里面对方的回答中,我们使用 NG-重复
指令为模板创建一个新的范围。
In the other answer that you linked to inside the comments, we use the ng-repeat
directive to create a new scope for the template.
或许,你可以通过包装数组中的父
属性 [...] :
Perhaps, you could mimic this behavior with your data by wrapping your parent
property inside an array [...]
:
控制器
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.tree = {
label:"A",
parent:{
label:"B",
parent:{
label:"C"
}
}
};
});
HTML
<ul ng-repeat="field in [tree]" ng-include="'tree.html'"></ul>
模板
<li>
<div>{{field.label}} {{[field.parent]}}</div>
<ul ng-if="field" ng-repeat="field in [field.parent]" ng-include="'tree.html'"></ul>
</li>
下面是链接到plunker: http://plnkr.co/edit / 7shibX0leK1TXVl5kfPc?p = preVIEW
Here is the link to the plunker: http://plnkr.co/edit/7shibX0leK1TXVl5kfPc?p=preview
有一个更好的解决方案是创建你自己的 NG-包括
,并通过您的孩子反对。
A better solution would be to create your own ng-include
and pass your child object to it.