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

Time:01-10

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.

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" >
        <div >
        </div>
        <div>
            <div >
                <div >
                    Current Base Rent (Annual)
                </div>

CodePudding user response:

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"

CodePudding user response:

The condition has to be model.leaseApDto !== null && model.leaseApDto.length !== 0

  •  Tags:  
  • Related