我正在尝试使用 ng-repeat 来遍历嵌套的树状数组。
Plunkr Here
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
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'}]
}
]
}
}
}
})
我的问题是,我需要一种方法来确定我何时处于数组中特定级别的最后一项,但由于 Angular 通过深度优先来遵循递归,所以我无法使用 ng -repeat-start
和 ng-repeat-end
:
当然,预期的结果是:
我如何知道我是否已达到特定级别的末尾?
最佳答案
您可以使用 $last
来确定每次迭代结束的时间:
<div ng-show="$last" ng-repeat-end>End of repeater</div>
这给了我你问题中的后一个结果。
关于javascript - 将 ng-repeat-start 和 ng-repeat-end 与嵌套转发器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26048657/