17

I want to repeat a piece of HTML, multiple times in my template.

But I want it to be repeated at different places on my page. This means that ngFor is not the solution as the pieces would be repeated directly one after the other.

A 'working solution' would be to define a specific @Component for my repeated HTML, and do something like that :

<p>Whatever html</p>
<my-repeated-html></my-repeated-html>
<h4>Whatever</h4>
<my-repeated-html></my-repeated-html>

But I find it overkill to create a dedicated component for doing something like that, it has no functional meaning and is only required by the HTML structure I want to set up.

Is there really nothing in ng2 template engine to allow me to define an "inner template" and use it wherever I need it in the current template?

5
  • 2
    I don't get why you think a component is overkill. If there is no logic a component is hardly more than the template and a selector. How can it be simpler? Jun 7, 2016 at 10:20
  • For instance this could be simpler If I could define something like that <template name="chunc"><div>The html I want repeated</div></template> and the reuse it where I want <template-use name="chunc"/>. (this does not exists, just an example). But I understand that the design of ng2 is to put it in a component, ok.
    – ATX
    Jun 7, 2016 at 10:29
  • Only within the same component? Jun 7, 2016 at 10:31
  • Yes, this has no need to be available to other components.
    – ATX
    Jun 7, 2016 at 10:35
  • I added a Plunker to my answer. Jun 7, 2016 at 10:52

2 Answers 2

38

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

The recently added ngTemplateOutlet might be what you want

<template [ngTemplateOutlet]="templateRefExpression" [ngOutletContext]="objectExpression"></template>

It can currently be used like

<template #templateRef>
    <pre>{{self | json }}</pre>
</template>

<template [ngTemplateOutlet]="templateRef"></template>

A template can also be passed to a child component to be rendered there

@Component({
  selector: 'some-child',
  providers: [],
  template: `
    <div>
      <h2>Child</h2>
<template [ngTemplateOutlet]="template" ></template>
<template [ngTemplateOutlet]="template" ></template>
    </div>
  `,
  directives: []
})
export class Child {
  @ContentChild(TemplateRef) template:TemplateRef;
}

to be used like

<some-child>
  <template>
    <pre>{{self | json }}</pre>
  </template>
</some-child>

stackblitz example

Another Plunker example
that uses data passed as

<template [ngTemplateOutlet]="..." [ngOutletContext]="templateData"

This way ngOutletContext can be used in the template like

<template let-image="image">
 {{image}}

where image is a property of templateData

If $implicit is used

<template [ngTemplateOutlet]="..." [ngOutletContext]="{$implicit: templateData}"

the ngOutletContext can be used in the template like

<template let-item>
 {{item}}
6
  • 1
    Currently trying in a Plunker. Couldn't make it work yet. Jun 7, 2016 at 10:44
  • 2
    can't get the above solution working @GünterZöchbauer. I don't see anything being rendered
    – giwook
    Oct 6, 2016 at 19:50
  • @GünterZöchbauer nothing gets rendered in your example Plunkers
    – maxbellec
    Oct 25, 2016 at 8:41
  • it is possible to set template from file ?
    – Daredzik
    Nov 30, 2016 at 13:58
  • No. A workaround is to build a component at runtime like explained in stackoverflow.com/questions/34784778/… Nov 30, 2016 at 14:00
2
<campaign-channels-list (onItemSelected)="_onItemSelected($event)" [customTemplate]="customTemplate" (onDragComplete)="_onDragComplete($event)" [items]="m_blockList"></campaign-channels-list>
        <template #customTemplate let-item>
            <a href="#" [attr.data-block_id]="item.blockID">
                <i class="fa {{item.blockFontAwesome}}"></i>
                <span>{{item.blockName}}</span>
                <i class="dragch fa fa-arrows-v"></i>
                <span class="lengthTimer hidden-xs"> 
                    {{item.length | FormatSecondsPipe}}
                </span>
            </a>
        </template>

and in rx component:

<div class="sortableList">
                    <li (click)="_onItemSelected(item, $event, i)" *ngFor="let item of m_items; let i = index" class="listItems list-group-item" [ngClass]="{'selectedItem': m_selectedIdx == i}">
                        <template [ngTemplateOutlet]="customTemplate" [ngOutletContext]="{$implicit: item}">
                        </template>
                    </li>
                </div>

pay attention to:

[ngOutletContext]="{$implicit: item}"

as well as

<template #customTemplate let-item>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.