htmlangulartypescriptsafe-navigation-operator

Is there something like a Safe Navigation Operator that can be used on Arrays?


I have used Safe Navigation Operator for Objects to load on Asynchronous calls and it is pretty amazing. I thought I could reproduce the same for Arrays but it displays a template parse error in my Angular code. I know *ngIf is an alternative solution, but is there a more simpler(by code) way just like the Safe Navigation Operator?

<div class="mock">
   <h5>{{data?.title}}</h5>  //This works
   <h6>{{data?.body}}</h6>  //This works
   <h6>{{simpleData?[0]}}</h6>  // This is what I tried to implement    
</div>

Solution

  • is there a more simpler(by code) way just like the Safe Navigation Operator?

    There is ternary operator.

    condition ? expr1 : expr2

    <h6>{{simpleData?simpleData[0]:''}}</h6>