1

I need to get the first element of an ngfor from the component to get it's offset value.

the only thing I've found so far is using let first = first; in the template which does not help

I've a reference to my list but I don't know how to use it for that :

@ViewChild('list') list: ElementRef;

How can I do that ?

An-droid
  • 6,433
  • 9
  • 48
  • 93
  • Where do you need to access it? In the template or in the ts? – bugs Jul 25 '18 at 14:34
  • As in the title, from the component.ts – An-droid Jul 25 '18 at 14:36
  • It'd be useful to see some code. – bugs Jul 25 '18 at 14:37
  • 1
    You need `@ViewChildren` - https://stackoverflow.com/questions/40165294/access-multiple-viewchildren-using-viewchild – Shardul Jul 25 '18 at 14:40
  • I'll add some but it's that basic of the basic. an empty component with only an array, a template with only a ngfor. that display the array. I'm trying to access the first element of the ngfor to get the offset – An-droid Jul 25 '18 at 14:42
  • If your component already has the array you use in the `ngFor`, can you not just do `array[0]` ? – bugs Jul 25 '18 at 14:44
  • array[0] would give me the data displayed, what I'm trying to get is the element from the dom. @sharedul that is the correct one thank you ! You can answer if you want – An-droid Jul 25 '18 at 15:05

0 Answers0