Search code examples
angulartypescriptangular2-componentsangular-ng-if

Angular2 check if object has peoperty inside *ngIf


export interface Element {    
    name: string;   
}

export class Room implements Element {
name: string; 
type:string
}

export class Hall implements Element {
name: string;
}

and my varibale type is like below

selectedElement: Element;

now in html how can I check if the object is having property 'type' or not?

<div *ngIf="selectedElement?.type">
my div
</div>

Solution

  • I guess this should do what you want:

    *ngIf="hasProp(selectedElement, 'type')"
    
    hasProp(o, name) {
      return o.hasOwnProperty(name);
    }