mr_blond
mr_blond

Reputation: 1730

Angular ngFor renders nothing if array[0] = undefined

There is an array:

var stringArray = new Array();
stringArray[1] = 'one';

And ngFor renders nothing in angular when stringArray[0] = undefined.

How do I solve it?

Upvotes: 0

Views: 517

Answers (2)

BizzyBob
BizzyBob

Reputation: 14750

If you want to exclude undefined, null and empty values from your template, you could simply filter your data source:

  private source = [
    undefined, 
    null, 
    "", 
    "Hello World"
  ];

  public stringArray = source.filter(i => i);

StackBlitz

screenshot of filtered output

Upvotes: 1

Nadhir Falta
Nadhir Falta

Reputation: 5267

That should work fine. I used ng-container for loop with ngFor and ngIf in a < li >
I was able to replicate that in stackblitz here is the link:

https://stackblitz.com/edit/angular-a2xph9?file=src%2Fapp%2Fapp.component.html

Upvotes: 1

Related Questions