简体   繁体   English

Angular6创建模板并将模板从父组件传递给子组件

[英]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.

相关问题 当父组件不使用子组件选择器时,如何将数据从子组件传递到Angular6中的父组件? - How to pass data from child to parent component in Angular6 when parent component not using child's component selector? Angular 6 在父组件中显示来自子组件的模板 - Angular 6 Show template from child in parent component 将数组从父组件传递给子 angular - pass array from parent component to child angular 将变量从父组件传递到子组件 angular - pass variable from parent to child component angular 将组件从子级传递到角度2中的父指令 - Pass a component from child to a parent directive in angular 2 在 Angular 中将数组从父组件传递到子组件 - Pass array from parent to child component in Angular 在Angular 2中将函数从Parent传递给Child组件 - Pass function from Parent to Child component in Angular 2 如何在angular2中将变量从子组件模板传递到父组件? - How to pass a variable from a child component template to a parent component in angular2? Angular6-初始化父级时如何初始化子级组件? - Angular6 - How to initialize child component when parent is initialized? 将数据从子级传递到父级组件Angular2(反应式和模板驱动相结合) - Pass data from child to parent component Angular2 (reactive and template driven combined)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM