如何动态加载自定义组件到div? Angular2

时间:2016-10-20 09:15:15

标签: html css angular angular2-forms

ANGULAR2 下, 我想将所有自定义组件加载到基于customComponentsList的html div中。

我的实际情况是我有的意思

A(0-row,0-column),B(0,1),C(0,2),D(1,0),E(1,1),F(2,0), G(2,1), H(2-row,2-column)) 

自定义组件。

我必须按行和列位置显示div,如

    A B C
    D E
    F G H

1 个答案:

答案 0 :(得分:0)

这可能有效

<div #target x="0" y="0"></div>
<div #target x="1" y="0"></div>
<div #target x="2" y="0"></div>

<div #target x="0" y="1"></div>
<div #target x="1" y="1"></div>
<div #target x="2" y="1"></div>

<div #target x="0" y="2"></div>
<div #target x="1" y="2"></div>
<div #target x="2" y="2"></div>
    
@ViewChildren('target', {read: ViewContainerRef}) targets: QueryList<ViewContainerRef>;

// this.targets is not filled before `ngAfterViewInit()` was called
addComponent(x, y) {
  let t = this.targets(y * 3 + x); 
}