I came across a strange assignment syntax inside an Angular 2 template.
<template let-col let-car="rowData" pTemplate="body">
<span [style.color]="car[col.field]">{{car[col.field]}}</span>
</template>
It appears that let-col
and let-car="rowData"
create two new variables col
and car
that can then be bound to inside the template.
Source: https://www.primefaces.org/primeng/#/datatable/templating
What is this magical let-*
syntax called?
How does it work?
What is the difference between let-something
and let-something="something else"
?
Can the above code be rewritten using <ng-container>
instead of <template>
without changing the final DOM structure?
update Angular 5
ngOutletContext
was renamed tongTemplateOutletContext
See also https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29
original
Templates (
<template>
, or<ng-template>
since 4.x) are added as embedded views and get passed a context.With
let-col
the context property$implicit
is made available ascol
within the template for bindings. Withlet-foo="bar"
the context propertybar
is made available asfoo
.For example if you add a template
See also this answer and ViewContainerRef#createEmbeddedView.
*ngFor
also works this way. The canonical syntax makes this more obviouswhere
NgFor
adds the template as embedded view to the DOM for eachitem
ofitems
and adds a few values (item
,index
,odd
) to the context.See also Using $implict to pass multiple parameters