2016-06-06 13 views
6

Mam więc składnik o <template>angular2 dane karmienia powrotem do `<template>` z `[ngTemplateOutlet]`

<some-component [data]="someArray"> 
    <template>foo<template> 
</some-component> 

i to za pomocą tego zdobyć szablonu

@ContentChild(TemplateRef) 
public tmpl: TemplateRef<any>; 

który jest teraz używany w szablonie takim, jak teraz, chciałbym być ble wydrukować niektóre dane z item w oryginalnym szablonie, w zasadzie, aby móc to zrobić

<some-component [data]="someArray"> 
    <template>foo {{ item }}<template> 
</some-component> 

to możliwe w jakiś sposób?

Odpowiedz

11

Gdy to ląduje https://github.com/angular/angular/pull/9042 to będzie działać tak jak tego

<div *ngFor="let item of someArrayFromDataInput"> 
    <template 
    [ngOutletContext]="{ 
     item: item 
    }" 
    [ngTemplateOutlet]="tmpl"></template> 
</div> 

+

<some-component [data]="someArray"> 
    <template let-item="item">foo {{ item }}<template> 
</some-component> 

// EDIT: wylądował

+1

Czy istnieje sposób "automatycznego" umieszczenia elementu 'let-item =" "" na komponencie? lub jeszcze lepiej zrobić to, co robi ngFor, i rozwinąć 'someDir =" foo "' do 'let-foo =" item "' dla poprawnego aliasingu? – TDaver

0

zmiana kątowa 5

ngOutletContext został przemianowany na ngTemplateOutletContext

Zobacz także https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29

oryginalny

Wygląda prosicie współpracujący https://github.com/angular/angular/issues/8368 tej samej funkcji (NgTemplateOutlet)

Aktualizacja

Zobacz How to repeat a piece of HTML multiple times without ngFor and without another @Component do pracy przykłady Plunker.

+0

Tak, https://gitter.im/angular/angular? at = 575544a4e96484d0692fe9c0 – foxx

Powiązane problemy