Angular 4 access template element without template local variable

Viewed 1320

I have a parent component (say ParentComponent) and I need to add many children components (say ChildComponent1, ChildComponent2, ... ChildComponentN) to it dynamically. Each component is different.

I followed this to solve my problem : Angular 2 RC5-6 - Dynamically insert a component

But in order to access the template element (a simple <div>) that will hold my ChildComponents through @ViewChild, I need to set a template local variable on this <div> element.

However, in my case, I can't do that because I can't set a template local variable with a dynamically name. So my <div> element is only characterized by an unique id attribute that is added dynamically (like <div id="child1">).

Is there any way to access my <div> element through the component implementation in typescript without template local variable ?

2 Answers
Related