Angular 2.1在指令内动态附加组件

时间:2016-10-19 17:54:23

标签: angular typescript

我是角度2.1的初学者。我想为自动翻译装饰一些元素,我将使用一个指令,它必须看起来像:

  .with( an_object_satisfying { |o| 
           o.slice(some_params) == ActionController::Parameters.new(some_params)
         })

或只是

<span customDirectiveTranslation="translateble">{{translateble}}</span>

我可以使用Renderer里面的指令类(createElement),但通过这种方式我只能添加像span这样的简单元素。

如何通过自定义指令在另一个组件中呈现组件? 感谢名单

1 个答案:

答案 0 :(得分:1)

constructor(
    private target:ViewContainerRef, 
    private componentFactoryResolver: ComponentFactoryResolver, 
    private compiler: Compiler) {}

ngAfterViewInit() {
  this.createComponent();
}

createComponent() {
    let factory = this.componentFactoryResolver.resolveComponentFactory(MyDynamicComponent);
    this.cmpRef = this.target.createComponent(factory)
}

另见Angular 2 dynamic tabs with user-click chosen components