-1

When you check just for variable then this code is enough

<div class="someComponent" *ngIf="someVariable">

But how can I use a specific component of array instead of variable? for example I want to check for array component someArray[] using idVar someArray[idVar]? How can I put this inside ngIf?

I have in my ts file someArray: Boolean[];

then in my template I have some loop where I constract divs with *ngIf="someArray[idVar]" because someArray has actual arguments for this moment there was error Cannot read property '1' of undefined then I wrapped everything in <ng-container *ngIf="someArray"> so when I load the page , because someArray do not exists everything works fine. but then I have button on press I transfer 1 to ts file and put 'true' value to someArray[1]. When I do so script goes on error Cannot read property '1' of undefined

David
  • 4,332
  • 13
  • 54
  • 93
  • What's wrong with `someArray[idVar]` signature? It should work. Essentially the statements are interpreted as TS expressions, so accessing an element in the array should also work. – ruth Aug 29 '20 at 10:51
  • As long as `someArray` and `idVar` are defined in your component's ts file, the expression `someArray[idVar]` should work. Are you getting any specific erros when trying this? – igg Aug 29 '20 at 10:56
  • Updated uestion – David Aug 29 '20 at 18:36
  • I have a loop where I put divs. and each div must be hidden. For that I use ngIf and I wat to bind that with array with corresponding number. and by press of a button in functioin I will make specific array component true so that div show up. Thats what I want – David Aug 29 '20 at 18:38

1 Answers1

0

I guess you are using the *ngFor directive. You can write let i = index inside the *ngFor like *ngFor ="let element of elements; let i = index" in order to have access on the iterator of the element. With this logic you can achieve what you want.

If this doesn't work for you, you have to be more accurate about the idVar. What is its scope? Is it somehow defined in the template or can you retrieve it by using a function inside the .ts file?

Alex Rasidakis
  • 302
  • 3
  • 12
  • no sir. I have a loop where I put divs. and each div must be hidden. For that I use ngIf and I wat to bind that with array with corresponding number. and by press of a button in functioin I will make specific array component true so that div show up. Thats what I want – David Aug 29 '20 at 18:38