2016-03-31 210 views
0

如何在沒有ng-include的情況下使用Angular 2進行遞歸模板。我不明白爲什麼角2將不包括這個技能...遞歸模板Angular2

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

+0

只要創建一個組件,並在裏面做。你這樣做的意義何在? –

+0

您可以使用組件中的組件來執行此操作。使用ngFor作爲ng-repeat和「call」組件的細節。 – IceManSpy

+0

我在http://jilles.me/ng-repeat-in-angular2-ng-for/找到解決方案 – vivi94

回答