好吧,我做到了。一切似乎都很好。
所以我使用compileModuleAndAllComponentsAsync
(就像它在這裏完成:https://github.com/patrikx3/angular-compile)。
爲了讓我的組件和指令可以在編譯的元素中進行綁定,我只需將它們放在我創建的NgModule
的declarations
中以編譯我的元素。
private async createFactory(opts: CompileOptions) {
const cacheKey = opts.template;
if (Object.keys(cache).indexOf(cacheKey) > -1) {
return cache[cacheKey];
}
cache[cacheKey] = new Promise(async(resolve) => {
@Component({
template: opts.template
})
class TemplateComponent {
context: any
}
@NgModule({
imports: opts.imports,
declarations: [
TemplateComponent
// <== Insert your directives and components here
]
})
class TemplateModule {
}
const component = await this.compiler.compileModuleAndAllComponentsAsync(TemplateModule);
const factory = component.componentFactories.find((comp) =>
comp.componentType === TemplateComponent
);
if (opts.onCompiled) {
opts.onCompiled(component);
}
cache[cacheKey] = factory;
resolve(factory);
})
return cache[cacheKey];
}
謝謝大家!
總是檢查文檔:https://angular.io/docs/ts/latest/cookbook/dynamic-component-loader.html – wannadream
http://stackoverflow.com/questions/40060498/angular-2-1-0 40080290 – yurzui
就像我在我的帖子中說過的,我已經試過了ComponentFactoryResolver並沒有幫助,但是謝謝:) – NhlstcR