0

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'
    }
  ];
});

3
  • Just create a component and do it inside that. what's your point in doing this? Commented Mar 31, 2016 at 9:52
  • You can use component in component to do this. Use ngFor as ng-repeat and "call" component with details. Commented Mar 31, 2016 at 9:53
  • I find a solution on jilles.me/ng-repeat-in-angular2-ng-for Commented Mar 31, 2016 at 14:48

1 Answer 1

1

Use ngFor instead of ng-repeat in Angular2.

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.