更新Angular 5
ngOutletContext 被重命名为 ngTemplateOutletContext
另请参见https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29
原版的
模板(<template>,或<ng-template>从4.x开始)被添加为嵌入式视图,并通过了上下文。
使用let-colcontext属性$implicit可以col在绑定模板中使用。使用let-foo="bar"context属性bar可以作为foo。
例如,如果您添加模板
<ng-template #myTemplate let-col let-foo="bar">
<div>{{col}}</div>
<div>{{foo}}</div>
</ng-template>
<!-- render above template with a custom context -->
<ng-template [ngTemplateOutlet]="myTemplate"
[ngTemplateOutletContext]="{
$implicit: 'some col value',
bar: 'some bar value'
}"
></ng-template>
另请参见此答案和ViewContainerRef#createEmbeddedView。
*ngFor也可以这样 规范的语法使这一点更加明显
<ng-template ngFor let-item [ngForOf]="items" let-i="index" let-odd="odd">
<div>{{item}}</div>
</ng-template>
其中NgFor添加模板作为嵌入视图到DOM每个item的items,并增加了几个值(item,index,odd)上下文。
另请参见使用$ implict传递多个参数