简体   繁体   中英

check with ngIf an array component angular

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM