Search code examples
angularangular7

comparing null for a number type in angular 7


html

<select class="form-control form-control-sm" formControlName="appDeptId" [(ngModel)]="appDeptId"
                    (change)="getAppClinics()">
                    <option [value]=null>Please Select</option>
                    <option *ngFor="let type of appDepartments" [value]="type.deptId">{{type.deptName}}
                    </option>
              </select>

ts

appDeptId: number;


getAppClinics(){
 if (this.appDeptId != null) {  // if fails for null
      console.log("this.appDeptId :"+this.appDeptId); // this displays null
}

Here even though the value of appDeptId id null, it still enters the if condition and prints null. How can I solve this.


Solution

  • You if condition is get null as string so you are comparing null!='null' actually so change your condition to

    if(deviceValue != null && deviceValue != 'null'){ 
    

    should work

    demo here