I want to get a list of native elements and related components to them from view.
I'll try to do something like that, but it's not work:
@ViewChildren('element', { read: [ElementRef, MyComponent] }) private elements: QueryList<any>; // <-- not work
or
@ViewChildren('element', { read: MyComponent }) private elements: QueryList<MyComponent>;
...
this.elements.first.nativeElement // <-- undefined
This work, but it doesn't look right:
@ViewChildren('element', { read: ElementRef }) private elements: QueryList<ElementRef>;
@ViewChildren('element', { read: MyComponent}) private components: QueryList<MyComponent>;
my template short example:
<virtual-scroller #scroll>
<my-component #element *ngFor="let c of components"></my-component>
</virtual-scroller>
One way to do this is to inject:
@ViewChildren('element', { read: MyComponent}) private components: QueryList<MyComponent>;
in parent component and expose elementRef
from child:
class MyComponent {
constructor(public elementRef: ElementRef) {}
}
and then just access elementRef
directly:
this.components.forEach(component => component.elementRef);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With