[英]Angular6 create and pass a template from a parent component to a child
I'm pretty new to angular, and am trying to create a custom table component to reuse throughout my application. 我对angular非常陌生,并且正在尝试创建自定义表格组件以在整个应用程序中重用。 And I have a simple example working by passing an array of data, and then an array of Column
configurations. 我有一个简单的示例,先传递数据数组,然后传递Column
配置数组。
My Column
config looks like 我的Column
配置看起来像
export interface Column {
field: string;
displayValue?: string;
cellTemplate?;
}
and then in my TableComponent
I build the table based off of the data and array of Columns passed in. 然后在我的TableComponent
中,根据传入的Columns的数据和数组构建表。
But now I want to try and make it a little more advanced, and be able to pass in the cellTemplate, something like 但是现在我想尝试使其更高级,并能够传递cellTemplate,例如
{
field: 'firstName',
displayValue: 'First Name',
cellTemplate: '<h1>My Name is {{cell}}</h1>
}
And then even a step further and be able to pass an onlclick event from the parent with it... 然后走得更远,并能够传递父级的onlclick事件...
{
field: 'firstName',
displayValue: 'First Name',
cellTemplate: '<button (click)="parentClick(cell)">Click Me to find out my name</button>'
}
...
parentClick(cell){
alert('My Name is ' + cell);
}
...
But I'm kind of stuck. 但是我有点卡住了。 I've found I can pass a static template like <h1>Hello World</h1>
and then use [innerHtml]="cellTemplate" but that's all I can figure out so far 我发现我可以传递诸如<h1>Hello World</h1>
类的静态模板,然后使用[innerHtml] =“ cellTemplate”,但这是到目前为止我能确定的全部
If you want to change template content using an expression or value, you can pass any value from parent component to child component by using @Input. 如果要使用表达式或值更改模板内容,则可以使用@Input将任何值从父组件传递到子组件。 But if you want to pass static template you can use content projection. 但是,如果要传递静态模板,则可以使用内容投影。 Pleaes follow this link how to use content projection. 请点击此链接,了解如何使用内容投影。 https://www.infragistics.com/community/blogs/b/infragistics/posts/simplifying-content-projection-in-angular https://www.infragistics.com/community/blogs/b/infragistics/posts/simplifying-content-projection-in-angular
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.