如何使用ngForTemplate包装模板? [英] How to wrap template using ngForTemplate?
本文介绍了如何使用ngForTemplate包装模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用ngForTemplate
作为自定义模板制作ListView组件
I try to make ListView component using ngForTemplate
for custom templates
list-view.component.html
<div class="list-view">
<template ngFor [ngForOf]="items" [ngForTemplate]="template">
</template>
</div>
list-view.component.ts
@Component({
selector: 'm-list-view',
styleUrls: ['./list-view.component.less'],
templateUrl: './list-view.component.html'
})
export class ListViewComponent {
@Input() items: any[] = [];
@ContentChild(TemplateRef)
template: any;
}
当我这样使用它时:
<m-list-view [items]="categories">
<template let-item="$implicit">
**some HTML markup**
</template>
</m-list-view>
结果是:
<m-list-view>
<div class="list-view">
**some HTML markup**
**some HTML markup**
**some HTML markup**
...
</div>
</m-list-view>
但是我需要这个:
<m-list-view>
<div class="list-view">
<div class="list-view-item">**some HTML markup**</div>
<div class="list-view-item">**some HTML markup**</div>
<div class="list-view-item">**some HTML markup**</div>
...
</div>
</m-list-view>
我该怎么做才能将每个列表视图项目模板包装在div.list-view-item中?
What should I do to wrap each list view item template in div.list-view-item?
推荐答案
更新Angular 5
ngOutletContext
重命名为ngTemplateOutletContext
现在建议您使用以下语法:
It is now advised that you use the following syntax:
<div class="list-view">
<div class="list-view-item" *ngFor="let item of items">
<ng-container *ngTemplateOutlet="template; context: item">
</ng-container>
</div>
</div>
另请参见 https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29
原始
<div class="list-view">
<div class="list-view-item" *ngFor="let item of items">
<template [ngTemplateOutlet]="template" [ngOutletContext]="item"></template>
</div>
</div>
另请参见 https://angular.io/docs/ts/latest/api/common/index/NgTemplateOutlet-directive.html
这篇关于如何使用ngForTemplate包装模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文