7
votes

So I have a component with a <template>

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

and it's using this to get hold of the template

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

which is then used in its template like this

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

now I would like to be able to print some data from item in the original template, basically to be able to do this

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

is it possible somehow?

2

2 Answers

12
votes

Updated to match Angular 5+ api ngOutletContext was renamed to ngTemplateOutletContext as per https://stackoverflow.com/a/37654077/301596


Once this lands https://github.com/angular/angular/pull/9042 it will work like this

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

+

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

// edit: landed

0
votes

update Angular 5

ngOutletContext was renamed to ngTemplateOutletContext

See also https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29

original

Looks like you are asking for the same feature requested in https://github.com/angular/angular/issues/8368 (NgTemplateOutlet)

Update

See How to repeat a piece of HTML multiple times without ngFor and without another @Component for working Plunker examples.