Problem with * ngIf: Can not read property 'indexvariacaoatributo' of undefined

2

I have a reactive form and I need to show this card only when in my array listAtributos[i].indexvariacaoatributo is different from null . It turns out that there is initially no indexvariacaoatributo , so it returns:

  

Can not read property 'indexvariacaoatributo' of undefined

What I've tried:

<ng-container *ngFor="let item of variacoes.controls; let i = index;">
<div *ngIf="listAtributos[i].indexvariacaoatributo !== undefined && listAtributos[i].indexvariacaoatributo !== null" class="animacaogeral animated zoomIn">

Is there any alternative for me to just show this card when my property in my array (indexvariance attribute) is different from null before it exists?

    
asked by anonymous 12.11.2018 / 17:47

1 answer

0

I think the problem is not in indexvariacaoatributo , probably in listAtributos[i] , which for some reason is undefined in some element.

Try this, maybe resolve:

<ng-container *ngFor="let item of variacoes.controls; let i = index;">
<div *ngIf="listAtributos[i] && listAtributos[i].indexvariacaoatributo" class="animacaogeral animated zoomIn">

In short, first check if listAtributos[i] exists and then check if listAtributos[i].indexvariacaoatributo exists, if the item is null , '' , 0 or undefined it will return false in if %, operador ternário , and so on.

    
12.11.2018 / 18:19