【发布时间】:2015-08-30 02:03:23
【问题描述】:
问题
如何在父组件中创建子组件,然后使用 Angular2 在视图中显示它们?如何确保注入物正确注入子组件?
示例
import {Component, View, bootstrap} from 'angular2/angular2';
import {ChildComponent} from './ChildComponent';
@Component({
selector: 'parent'
})
@View({
template: `
<div>
<h1>the children:</h1>
<!-- ??? three child views shall be inserted here ??? -->
</div>`,
directives: [ChildComponent]
})
class ParentComponent {
children: ChildComponent[];
constructor() {
// when creating the children, their constructors
// shall still be called with the injectables.
// E.g. constructor(childName:string, additionalInjectable:SomeInjectable)
children.push(new ChildComponent("Child A"));
children.push(new ChildComponent("Child B"));
children.push(new ChildComponent("Child C"));
// How to create the components correctly?
}
}
bootstrap(ParentComponent);
编辑
我在API docs preview 中找到了DynamicComponentLoader。但是在执行示例时出现以下错误:在元素 0 处没有动态组件指令
【问题讨论】:
-
感谢您提出这个问题——这正是我想要弄清楚的。
标签: javascript angular typescript