0

In Angular I have an ngFor loop iterating over an array of form groups. The loop looks like this:

<div *ngFor="let fg of arrayOfFormGroups" [attr.fg]="fg">
</div>

Would it somehow be possible to access this fg with pure JavaScript? When I do the following in the Chrome console:

nativeElement.attributes.getNamedItem('fg').value;

I only get "[object Object]" string value.

Is what I am trying to achieve possible?

The reason why this is relevant is that I'm developing keyboard navigation, and using the nextElementSibling attribute would make it a breeze, but I need to get hand of this fg.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
DauleDK
  • 3,313
  • 11
  • 55
  • 98
  • is `fg` an object? – brk Jun 14 '20 at 11:18
  • Are you integrating the Angular element in a pure JS context? If not you could try with template reference variables. – ruth Jun 14 '20 at 11:18
  • Yes, `fg` is an Angular Form Group. @MichaelD this question came up when brainstorming how to develop some navigation features, where the sibling attributes could be very useful (if I could access the attributes these native elements have) – DauleDK Jun 14 '20 at 11:26
  • See the answer for this question: https://stackoverflow.com/questions/35233572/how-to-access-angular2-component-specific-data-in-console – Elias Soares Jun 14 '20 at 11:39
  • Thanks for providing a link @EliasSoares, but I'm not interested in debugging here. – DauleDK Jun 14 '20 at 11:44
  • Is not debugging. You use ng.probe to get the component related to that element and directly access the value of the variable you want. – Elias Soares Jun 14 '20 at 11:52
  • problem is that won't work in production. – DauleDK Jun 14 '20 at 11:54
  • 1
    You cannot store an object in an attribute, only a string. So you could do a JSON.stringify. But i’s probably better to rethink your approach. – MikeOne Jun 14 '20 at 12:42

0 Answers0