Pass variable in Angular 2 template
Asked Answered
P

5

34

Is there a way I can pass variables to templates in Angular2?

Let's say I have the following code:

<div *ngFor="foo in foos">
    <ng-container *ngTemplateOutlet="inner"</ng-container>
</div>

---------------

<ng-template #inner>
    {{ foo.name }}
</ng-template>

How can I get the template to print the name of foo?

Patagonia answered 1/5, 2017 at 21:25 Comment(0)
A
56

You should do like this:

<div *ngFor="foo in foos">
   <ng-container *ngTemplateOutlet="inner; context:foo"></ng-container>
</div>

<ng-template #inner let-name="name">
   {{ name }}
</ng-template>
Attenuation answered 1/5, 2017 at 21:47 Comment(1)
But this doesn't work if the template is located in another component. How do you pass a context object in such scenario?Transposition
Y
27

in my case I needed the object to remain intact because I had some kind of recursion, this worked

<div *ngFor="foo in foos">
   <ng-container *ngTemplateOutlet="inner; context: {foo : foo}"></ng-container>
</div>

<ng-template #inner let-foo="foo">
   {{ foo.attributexy }}
</ng-template>
Yeeyegg answered 1/8, 2019 at 1:0 Comment(1)
For me this answer worked instead of the accepted one. I am working with Angular 8, maybe this is the reason.Mvd
P
7

But this doesn't work if the template is located in another component. How do you pass a context object in such scenario?

I added
@Input() itemTemplate: TemplateRef<any>;

in component where I will use it, and in template of this component I write something like this:

  <ng-container *ngTemplateOutlet="itemTemplate; context: item"></ng-container>

Code of template from outside component:

<ng-template #dataRendererTpl let-data="data">
<div class="data">Hello, {{data.name}}</div>

Just pass link to dataRendererTpl as @Input() property to component in which you need it

Pisano answered 26/12, 2018 at 10:49 Comment(0)
W
3

In the case you want to send the whole iterable object to the template, you can pass the array into the context as follow:

<ng-container *ngTemplateOutlet="inner; context:foos"></ng-container>

<ng-template #inner let-foos="values()">
   <div *ngFor="foo in foos">
      {{ foo.name }}
   </div>
</ng-template>
Wedded answered 30/1, 2019 at 12:37 Comment(0)
E
1

Let's make use of the $implicit key in the context object of ngTemplateOutletContext or ngTemplateOutlet, as it will set its value as default.

<div *ngFor="let foo of foos">
   <ng-container *ngTemplateOutlet="inner; context: { $implicit: foo}"></ng-container>
</div>

<ng-template #inner let-foo>
   {{ foo.name}}
</ng-template>
  1. This will help us in accessing the whole object ie Multiple parameters inside the template.
  2. Then, there is no dependency that let-foo should match the context object default value variable name (foo). It can be any variable.
    <ng-template #inner let-user>
        {{ user.name}}
    </ng-template>

I'm Assuming, ng-template and div with ng-container are in same component.

If the template is in other component, make use of @input() decorator inside template component.ts

Engenia answered 7/1, 2022 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.