Angular 2:ngFor with dynamic templates / components

时间:2016-03-15 17:46:00

标签: angular angular2-directives

我希望熟悉ng2内部工作的人,尤其是动态组件加载器和视图容器,能够帮助我解决一个不寻常的问题。我们正在尝试创建一个带有一些相同的ngFor变化检测语义的指令(也就是说,它将它绑定到一个集合,它会在集合更改时添加/删除子元素/组件时做出反应)而不是简单地重复一个模板X的次数,我们需要能够动态加载(和配置)单个组件。例如,如果PageComponent定义如下:

export interface PageComponent {
    /**
     * The name of the component. Must be unique.
     */
    name: string;
    /**
     * The category to which the component belongs
     */
    category: ComponentCategory;
    /**
     * The conditions under which the component should be displayed/hidden
     */
    displayConditions?: Array<DisplayCondition>;
    /**
     * The list of inputs for the component.
     * It as a map of the input name (unique per component) to the associated 
     * input metadata
     */
    inputs?: {[key: string]: InputMetadata};
    /**
     * The child components, if any, that comprise this @link(PageComponent)
     */
    components?: Array<PageComponent>;
}

我们需要指令来获取PageComponents数组并呈现生成的组件(假设存在查找服务以从组件名称映射到组件类型)。看看ng2源代码中ngFor的实现,除了依赖提供模板(它创建一个EmbededView)之外,它还有很多我们需要的东西,因为我们可能需要使用DCL来创建一个HostView。 有谁知道如何实现这一目标?

0 个答案:

没有答案