Angular 2 and dynamic template (remote content)
I need to load a dynamic template (templates written by java backend). So I tried 2 solutions 1) componentfactory (fe: https://netbasal.com/dynamically-creating-components-with-angular-a7346f4a982d ) 2) ng dynamic module: https://www.npmjs.com/package/ ng-dynamic
The second is simpler, but the problem is that I cannot get references to the generated components. How about such a function?
+3
source to share
1 answer
This is how I solved my problem:
In each component class, I put an injector to be used in ngOnInit()
constructor(private _inj: Injector){}
I get a reference to the parent component (the component that uses the dynamic component) and registers the component (there is an array of the child component in the parent component)
ngOnInit() {
let parentComponent = this._inj.get(ParentComponent);
if (this.parentComponent) {
this.parentComponent.registerChild(this);
}
}
0
source to share