使用ng-repeat-start和ng-repeat-end与嵌套转发器
16lz
2021-01-22
I'm trying to use ng-repeat to go through a nested tree-like array.
我正在尝试使用ng-repeat来浏览嵌套的树状数组。
Plunkr Here
HTML
HTML
<div layout></div>
<script type="text/ng-template" id="layout">
<div class="layout" ng-repeat-start="data in data.nodes" ng-include="data.name"></div>
<div ng-repeat-end>End of repeater</div>
</script>
<script type="text/ng-template" id="box">
<div class="box">Box</box>
</script>
JavaScript
JavaScript的
var myApp = angular.module('myApp', []);
myApp.directive('layout', function() {
return {
templateUrl: 'layout',
controller: function($scope) {
$scope.data = {
'nodes': [
{
'name': 'layout',
'nodes': [{'name': 'box'}, {'name': 'box'}, {'name': 'box'}, {'name': 'box'}]
},
{
'name': 'layout',
'nodes': [{'name': 'box'}, {'name': 'box'}, {'name': 'box'}, {'name': 'box'}]
}
]
}
}
}
})
My problem is that I need a way to figure out when I'm in the last item of a particular level in the array, but since Angular follows recursion by going deep-first, I can't use ng-repeat-start
and ng-repeat-end
:
我的问题是我需要一种方法来弄清楚当我在数组中特定级别的最后一项时,但是由于Angular通过深度优先跟随递归,我不能使用ng-repeat-start和ng -repeat端:
The expected result, of course, is:
当然,预期的结果是:
How can I know whether I am at the end of a particular level?
我如何知道自己是否处于特定级别的末尾?
1 个解决方案
#1
2
You could use $last
to determine when you're at the end of each iteration:
您可以使用$ last来确定每次迭代结束时的时间:
<div ng-show="$last" ng-repeat-end>End of repeater</div>
This is giving me the latter result in your question.
这会在你的问题中给我后一个结果。
更多相关文章
- 如何通过ajax将javascript数组传递给YII动作
- 在Javascript中将带有空格的字符串数组转换为小写,然后在Webkit中
- 返回指定时间段相同间隔数组
- JavaScript数组中的唯一计数,按计数排序
- Javascript sort()不适用于Chrome中超过10个对象的数组
- 获取JavaScript数组元素的最大长度
- 对多维数组中的列进行排序
- 获取拆分字符串数组的最后一个元素
- JS将字符串转换为数组