ng-if check if array is empty
post.capabilities.items
will still be defined because it's an empty array, if you check post.capabilities.items.length
it should work fine because 0
is falsy.
Verify the length
property of the array to be greater than 0
:
<p ng-if="post.capabilities.items.length > 0"> <strong>Topics</strong>: <span ng-repeat="topic in post.capabilities.items"> {{topic.name}} </span></p>
Arrays (objects) in JavaScript are truthy values, so your initial verification <p ng-if="post.capabilities.items">
evaluates always to true
, even if the array is empty.
To overcome the null
/ undefined
issue, try using the ?
operator to check existence:
<p ng-if="post?.capabilities?.items?.length > 0">
Sidenote, if anyone got to this page looking for an Ionic Framework answer, ensure you use
*ngIf
:<p *ngIf="post?.capabilities?.items?.length > 0">