gpt4 book ai didi

javascript - 递归模板 Angular2

转载 作者:行者123 更新时间:2023-12-03 07:18:03 25 4
gpt4 key购买 nike

如何在没有 ng-include 的情况下使用 Angular 2 执行递归模板。我不明白为什么 Angular 2 不包含这项技能...

HTML:

<div ng-app="app" ng-controller='AppCtrl'>
<script type="text/ng-template" id="categoryTree">
{{ category.title }}
<ul ng-if="category.categories">
<li ng-repeat="category in category.categories" ng-include="'categoryTree'">
</li>
</ul>
</script>
<ul>
<li ng-repeat="category in categories" ng-include="'categoryTree'"></li>
</ul>

JS:

var app = angular.module('app', []);
app.controller('AppCtrl', function ($scope) {
$scope.categories = [
{
title: 'Computers',
categories: [
{
title: 'Laptops',
categories: [
{
title: 'Ultrabooks'
},
{
title: 'Macbooks'
}
]
},
{
title: 'Desktops'
},
{
title: 'Tablets',
categories: [
{
title: 'Apple'
},
{
title: 'Android'
}
]
}
]
},
{
title: 'Printers'
}
];
});

最佳答案

在 Angular2 中使用 ngFor 代替 ng-repeat。

https://angular.io/docs/ts/latest/api/common/NgFor-directive.html

关于javascript - 递归模板 Angular2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36329899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com