Search code examples
angularangular2-templateangular2-directivesangular2-services

How to deal with multiple null or undefined values when any object or field can be null? like this <p>{{object.object2.object3.field1}}</p>


<p>{{object.object2.object3.field1}}</p>

I have many <p> filled with objects that have an object which have an object that might have field and in any case object or object2 or object3 or field can be null or undefined. How to deal with this correctly?

If I try to put an ngIf for each and every value my view will get crazy.


Solution

  • you may try below,

    <p>{{object?.object2?.object3?.field1}}</p>
    

    Read more about Safe navigation operator here.

    Hope this helps!!