如何通过角度2中的父组件将自定义(html)模板传递给子组件? [英] How to pass a custom (html) template to child component via parent component in angular 2?

查看:47
本文介绍了如何通过角度2中的父组件将自定义(html)模板传递给子组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        <grid
            [data]="sortedDeviceList"
            [multiSelect]="false"
            [enableSort]="true"
            [rowTemplate]="">
        </grid>

我想将HTML模板传递给rowTemplate属性.模板将如下所示.

I want to pass a HTML template to the rowTemplate property. And the template will look like this.

<div> 
 <div>{{item.id}}</div>
 <div>{{item.name}}</div>
</div>

网格模板看起来像这样.

The grid template look likes this.

<div *ngFor="let item in data">
// template should appear here.
</div>

该怎么做?

推荐答案

@Component({
  selector: 'grid'
  template: `
<div *ngFor="let item in data" *ngForTemplate="templateRef" *ngOutletContext="{$implicit: data}">
</div>`
})
export class GridComponent {
  @Input() data:any[];
  constructor(private templateRef:TemplateRef) {}
}

并像使用它

<template #myTemplate>
  <div> 
   <div>{{item.id}}</div>
   <div>{{item.name}}</div>
  </div>
</template

另请参阅

这篇关于如何通过角度2中的父组件将自定义(html)模板传递给子组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆