Angular2 - 将值传递给使用 ComponentFactory 创建的动态组件

Angular2 - Pass values to a dynamic component created with ComponentFactory

我听从了 Günter Zöchbauer 在 上的建议 我想知道如何将值传递给它 - 例如,子组件有一个 @input.

使用上述问题中给出的 plunker 示例 - plunker - 我如何向子组件传递一个字符串,我们称之为消息,然后在单击 'add' 按钮时显示该字符串。

下面是子组件的外观示例:

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){
      }
    }

你可以试试下面,

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

这是Plunker!!