2016-09-02 7 views
11

मैं इस तरह कुछ करना चाहता हूँ:कोणीय 2 में <template * ngFor के बराबर क्या है?

<div *ngFor="let parent of parents"> 
    <div *ngFor="let child of parent.children"> 
    </div> 
</div> 

लेकिन मुझे लगता है कि अतिरिक्त बाहरी div नहीं करना चाहती। मैं क्या खोज को खोजने के लिए, कोणीय 1 में कर लिया है से मैं ऐसा करने से इस लक्ष्य को हासिल कर सकते हैं:

<template *ngFor="let parent of parents"> 
    <div *ngFor="let child of parent.children"> 
    </div> 
</template> 

यह हालांकि कोणीय 2 में काम करने के लिए प्रतीत नहीं होता। ऐसा करने का सही तरीका क्या है?

+1

देखना कैसे आप सूचकांक मिलता है ('https://angular.io/docs/ts/latest/api/common/index/NgFor-directive.html –

उत्तर

17

मुझे लगता है कि यह आपके लिए काम करना चाहिए:

<ng-template ngFor let-parent [ngForOf]="parents"> 
    <div *ngFor="let child of parent.children"> 
    </div> 
</ng-template> 

आप और अधिक यहाँ पढ़ सकते हैं:

https://angular.io/docs/ts/latest/guide/template-syntax.html#!#star-template

इसके अलावा, आप अब उपयोग कर सकते हैं:

<ng-container *ngFor="let parent of parents"> 
    <div *ngFor="let child of parent.children"> 
    </div> 
</ng-container> 

लाभ यहां यह है कि यह एक सामान्य डोम तत्व के साथ एक ही वाक्यविन्यास है।

+2

की सिंटेक्स अनुभाग देखें मैं जाने = अनुक्रमणिका') उपरोक्त वाक्यविन्यास का उपयोग कर? – HGB

+2

कोणीय 4.x में यह 'एनजी-टेम्पलेट' – Loolooii

+1

एचजीबी है, इंडेक्स प्राप्त करने के लिए आप इस तरह से let-i = "अनुक्रमणिका" जोड़ देंगे:

5

<ng-container> बर्ताव करता है (यह डोम में नहीं जोड़ा जाता है), लेकिन उपयोग करने के लिए और अधिक सुविधाजनक *foo संरचनात्मक की अनुमति देता है निर्देश वाक्यविन्यास:

<ng-container *ngFor="let parent of parents"> 
    <div *ngFor="let child of parent.children"> 
    </div> 
</ng-container> 

मो के लिए फिर NgFor के बारे में विवरण https://angular.io/docs/ts/latest/api/common/index/NgFor-directive.html

संबंधित मुद्दे