2016-08-19 76 views
2

我遵循GünterZöchbauer的建议how to create dynamic components 我想知道如何将值传递给它 - 因为子组件具有@input。Angular2 - 将值传递给使用ComponentFactory创建的动态组件

使用上述问题中给出的plunker示例 - plunker - 如何将子组件传递给一个字符串,让它调用它的消息,然后单击“添加”按钮时将显示该消息。

这里的孩子组件如何看一个例子:

import {Component OnChanges, Input} from '@angular/core' 

    @Component({ 
     selector: 'hello', 
     providers: [], 
     template: `<h1>{{message}}</h1>`, 
     directives: [] 
    }) 
    export class HelloComponent implements OnChanges { 
     @Input() message:any; 

     constructor() { 
     } 

     ngOnChanges(changes:any){ 
     } 
    } 

回答

9

你可以试试以下,

let instance = this.viewContainerRef.createComponent(this.componentFactory, 0).instance; 
    instance.message = "some text!!"; 

这里是Plunker!!

希望这有助于!

+1

我想先传递一个虚拟数据来显示加载屏幕,然后一旦我从API获取数据,我想通过它。那可能吗 – indra257

相关问题