Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass different context to ngIf

Tags:

angular

Take the following template example. The Im creating a template context of user with the as syntax and would like to pass that context into the userTmpl. From what I can tell there's no way to pass context outside of what is used by the ngIf condition. Which in this example, is the showUser property.

<ng-container *ngIf="user$ | async as user">     <ng-container *ngIf="showUser; then userTmpl; else emptyTmpl"></ngcontainer> </ng-container> 

How can you pass the user template input variable to the nested templateRef?

Looking at the source, the ngIf directive looks like it sets the context to the condition of the ngIf. Im not aware of a way to basically override that context variable thats used within the templates

Something like this would be ideal, but not seeing a way to do it.

<ng-container *ngIf="showUser; then userTmpl; else emptyTmpl; context: user"></ngcontainer> 
like image 855
cgatian Avatar asked Aug 08 '17 14:08

cgatian


People also ask

Can I use ng-template without ngIf?

ng-template should be used along with structural directives like [ngIf],[ngFor],[NgSwitch] or custom structural directives. That is why in the above example the contents of ng-template are not displayed. ng-template never meant to be used like other HTML elements.

Can we use ngIf in ng container?

ng-container s work just like that, and it also accepts Angular structural directives ( ngIf , ngFor , e.t.c). They are elements that can serve as wrappers but do not add an extra element to the DOM.

What is * ngIf and how does it work?

NgIflink. A structural directive that conditionally includes a template based on the value of an expression coerced to Boolean. When the expression evaluates to true, Angular renders the template provided in a then clause, and when false or null, Angular renders the template provided in an optional else clause.


2 Answers

There are two very similar ways to solve this. I've created a stackblitz with the two solutions (tested with Angular 6.0.1 and the latest 6.1.8) for passing data to different templates based on a condition.

version #1

    <ng-container *ngTemplateOutlet="showUser ? userTmpl : emptyTmpl; context: { $implicit: user }">     </ng-container>      <ng-template #userTmpl let-user>         ...     </ng-template>      <ng-template #emptyTmpl let-user>         ...     </ng-template> 

version #2

Another possibility is to use @Ben's solution, but then you have to omit the let-user part (and fix the closing tag in his code). This way the code would look like this.

    <ng-container *ngTemplateOutlet="showUser ? userTmpl : emptyTmpl; context: user">     </ng-container>      <ng-template #userTmpl>         ...     </ng-template>      <ng-template #emptyTmpl>         ...     </ng-template> 
like image 78
coreuter Avatar answered Sep 30 '22 09:09

coreuter


Take a look at NgTemplateOutlet.

Something like that would work:

<ng-container *ngIf="user$ | async as user">     <ng-container *ngIf="showUser">       <ng-container *ngTemplateOutlet="userTmpl; context: { $implicit: user }"></ng-container>     </ng-container> </ng-container>  <ng-template #userTmpl let-user>   {{ user | json } </ng-template> 
like image 28
Edmundo Rodrigues Avatar answered Sep 30 '22 09:09

Edmundo Rodrigues