Skip to content

Instantly share code, notes, and snippets.

@Hypercubed
Created March 24, 2019 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hypercubed/cf65d4442946f9016d84d3562f267824 to your computer and use it in GitHub Desktop.
Save Hypercubed/cf65d4442946f9016d84d3562f267824 to your computer and use it in GitHub Desktop.
import { Injectable, Injector, Compiler } from '@angular/core';
import { SettingsService } from './settings.service';
@Injectable({
providedIn: 'root'
})
export class PluginsService {
constructor(
private settings: SettingsService,
private injector: Injector,
private compiler: Compiler) {
}
initPlugins() {
this.settings.plugins.forEach(plugin => {
if (typeof plugin === 'function' && plugin.ngInjectorDef) {
this.loadModule(plugin);
}
if (typeof plugin === 'object' && plugin.ngModule) {
this.loadModule(plugin.ngModule, plugin.providers);
}
});
}
private loadModule(module: any, providers?: any[]) {
this.compiler.compileModuleAsync<any>(module).then(moduleFactory => {
const injector = Injector.create({
providers,
parent: this.injector
});
moduleFactory.create(injector);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment