Accessing structural directive element from within a children class

262 views Asked by At

Is it possible to access parent (which is element of structural directive) from within a child element class?

For example, is it possible to access ngFor index, without passing it into all the children.

<ul>
    <li *ngFor="let elem of data; let i = index">
        <the-component></the-component>
        <span [the-directive]>Test</test> 
    </li>
</ul>

The reason why I am asking is that inside the for loop, I will have a lot of components and directives used and all of them will need index. I would like to ommit passing it to every each item.

In non-structural directive, we can inject the parent in the constructor like that:

constructor(
   el: ElementRef,
   private parent: ParentDirective
)

here, we will see all the values of the parent class instance. However, it does not seem to work with structural ones so accessing index is impossible.

Does creation of custom structural directives may help here?

0

There are 0 answers