How can I do recursive template with Angular 2 without ng-include. I don't understand why Angular 2 won't include this skill...
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' } ]; });
Use ngFor instead of ng-repeat in Angular2.
https://angular.io/docs/ts/latest/api/common/NgFor-directive.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.