Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how do we check a null value and a length at the same time in a template condition?

Basically I have a data that sometimes the value is null and sometimes the value is an empty array , it should still render the template if leaseApDto is not null or leaseApDto.length not equal to 0.

I tried to use the condition model.leaseApDto !== null || model.leaseApDto.length !=== 0 but they dont seem to work together even if its an OR condition.

Any idea ? would really appreciate it. Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

leaseApDto with a null value

enter image description here

leaseApDto with an empty value but is an array

enter image description here

#code

  <div *ngIf="model.leaseApDto !== null || model.leaseApDto.length !=== 0" class="transaction-lease-details-list">
        <div class="description-header">
        </div>
        <div>
            <div class="transaction-lease-details-content list-row-divider">
                <div class="transaction-lease-details-left-label">
                    Current Base Rent (Annual)
                </div>

>Solution :

For your case, wouldn’t replacing || with && resolve your issue?

Or you can simply use

*ngIf="model.leaseApDto?.length"

? is the safe navigation operator and helps to decrease a lot of code, and also helpful in such cases.

I would personally add the check to model too, like *ngIf="model?.leaseApDto?.length"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading