Search code examples
arraysjsonangularngforangular-ng-if

check with *ngIf, if there is an array or not (Angular 4)


I'm looping through a json file with *ngFor. While looping through the data, I want to check for every person, if it has no colors array. I do this with *ngIf.

JSON

[
  {
    "name": "Peter",
    "colors": [
      {
        "color": "blue"
      },
      {
        "color": "yellow"
      }
    ]
  },
  {
    "name": "Maria"
  }
  // has no colors array
]

HTML

<div *ngFor="let person of persons">
     <div *ngIf=" what comes here?? ">
        <p>{{person.name}} has no colors</p>
     </div>          
</div>

How can I check, if a person has no colors array?


Solution

  • Very simply :

     <div *ngIf="!person.colors">
         <p>{{person.name}} has no colors</p>
     </div>