2016-08-19 80 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

相關問題