【发布时间】:2017-10-11 12:58:21
【问题描述】:
我有这个小网格组件:
@Component({
selector: 'moving-grid',
templateUrl: './grid.component.html',
styleUrls: ['./grid.component.css']
})
export class GridComponent {
@Input('widgets') extComponents: Array<Component>;
}
还有第二个测试组件
@Component({
selector: 'test',
template: `
<div #content>I say hello<li>i</li><li>u</li><button (click)="test()">click me</button> world</div>
`
})
export class TestComponent {
@ViewChild('content') content: HTMLElement;
getContent() {
return this.content;
}
test() {
console.log("test");
}
}
现在我正在尝试将 testComponent 的多个实例传递给 gridComponent。因此,我有第三个组件,看起来像这样:
template: `
<moving-grid [widgets]="[z1,z2]">
<test class="grid-item-content-001" #z1></test>
<test class="grid-item-content-002" #z2></test>
</moving-grid>
`
到目前为止,一切都按预期进行。但是如何在 gridComponent 中从 @Input 渲染组件? 我的第一种方法是在 testComponent 中声明一个 @ViewChild 并使用 getContent() 函数返回它。但它不会起作用。我可以以某种方式使用 ng-content 指令还是有更好的解决方案?
GridComponent 看起来像这样。我想在其中一个黑框内显示 @Input-Component 的模板。有可能吗?
感谢您的帮助
【问题讨论】:
-
给未来的读者;您可能还想考虑动态组件加载器。 angular.io/guide/dynamic-component-loader
标签: angular typescript input nested components