ng2 – dynamically creating a component based on a template

Update2: As @estus mentioned in comments version with className won’t work with minification. To do it working with minification you can 1) add some static key on each of your entryComponents like: export LineChartComponent { static key = “LineChartComponent”; } and then use this key as unique. const factoryClass = <Type<any>>factories.find((x: any) => x.key === … Read more

Testing – Can’t resolve all parameters for (ClassName)

Using Jest? In case anyone gets here AND you’re using Jest to test your Angular app (hopefully we’re a growing minority), you will run into this error if you are not emitting decorators (“emitDecoratorMetadata”:true). You’ll need to update your tsconfig.spec.json file so it looks like: { “extends”: “../../tsconfig.json”, “compilerOptions”: { “emitDecoratorMetadata”: true, “outDir”: “../../out-tsc/spec”, “types”: … Read more

How to call component method from service? (angular2)

Interaction between components can be indeed achieved using services. You will need to inject the service use for inter-component communication into all the components which will need to use it (all the caller components and the callee method) and make use of the properties of Observables. The shared service can look something like this: import … Read more

Getting instance of service without constructor injection

In the updated Angular where ngModules are used, you can create a variable available anywhere in the code: Add this code in app.module.ts import { Injector, NgModule } from ‘@angular/core’; export let AppInjector: Injector; export class AppModule { constructor(private injector: Injector) { AppInjector = this.injector; } } Now, you can use the AppInjector to find … Read more