Question
What are practical scenarios of *ngTemplateOutlet directive?
I was reading about *ngTemplateOutlet
directive. The use of this directive is to instantiate a template dynamically by a template reference and context object as parameters.
What I want to know is that we have so many things in Angular to achieve the same results as *ngTemplateOutlet such as:
We can have multiple
*ngIf
which could render different templates based on the component variable value within the same component. In a similar fashion we have[ngSwitch]
which would render different templates for us based on different values.We could use references with
*ngIf
by referring to the template reference variable of the respective variable.
For the former case:
<div *ngIf="condition1"> Content 1 </div>
<div *ngIf="condition2"> Content 2 </div>
<div *ngIf="condition3"> Content 3 </div>
And for latter:
<ng-container *ngIf="condition then myTemplate else otherTemplate"></ng-container>
<ng-template #myTemplate> Some content... </ng-template>
<ng-template #otherTemplate> Some content... </ng-template>
If we have such methods in our arsenal what more value does *ngTemplateOutlet
add?
What are the practical use cases (if there are any) where we cannot use the above methods and should use *ngTemplateOutlet
directive or is it just another method to choose from to achieve the same result?