How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

后端 未结 15 1918
忘掉有多难
忘掉有多难 2020-11-21 05:14

I want to dynamically create a template. This should be used to build a ComponentType at runtime and place (even replace) it somewhere inside of the ho

15条回答
  •  鱼传尺愫
    2020-11-21 06:04

    Following up on Radmin's excellent answer, there is a little tweak needed for everyone who is using angular-cli version 1.0.0-beta.22 and above.

    COMPILER_PROVIDERScan no longer be imported (for details see angular-cli GitHub).

    So the workaround there is to not use COMPILER_PROVIDERS and JitCompiler in the providers section at all, but use JitCompilerFactory from '@angular/compiler' instead like this inside the type builder class:

    private compiler: Compiler = new JitCompilerFactory([{useDebug: false, useJit: true}]).createCompiler();
    

    As you can see, it is not injectable and thus has no dependencies with the DI. This solution should also work for projects not using angular-cli.

提交回复
热议问题