I am trying to create a component that will do some stuff and loop over a result set. I want to be able to provide a \"template\" for the items in the looped result set.
Solved this with the following:
Component template usage:
{{item.username}}
{{item.email}}
Component template definition:
0">
-
Component class:
@Component({...})
export class SearchFieldComponent {
@ContentChild(TemplateRef) templateRef: TemplateRef;
// ...
}
The explanation:
Using ng-template, I can use the let-item syntax, where item is the data that will be passed into the template on each iteration of the loop.
And in order to make the above possible, in the search-field component I use ng-template with ngTemplateOutlet as the template reference, and ngTemplateOutletContext is given the value {$implicit: item}, where item is the data I want to pass into the template.
Lastly, in the component class I need to use ContentChild to get the reference to the template to use in the ngTemplateOutlet.