export interface Element { name: string; } export class Room implements Element { name: string; type:string } export class Hall implements Element { name: string; }y mi tipo de variable es como a continuación
selectedElement: Element;ahora en html, ¿cómo puedo verificar si el objeto tiene la propiedad 'tipo' o no?
<div *ngIf="selectedElement?.type"> my div </div>Supongo que esto debería hacer lo que quieres:
*ngIf="hasProp(selectedElement, 'type')" hasProp(o, name) { return o.hasOwnProperty(name); }Puedes hacerlo simplemente en el html:
<div *ngIf="selectedElement.hasOwnProperty('type')"> my div </div>o
<div *ngIf="selectedElement.hasOwnProperty('type');then showMyDiv">...</div> <ng-template #showMyDiv> my div </ng-template>además de lo dicho por Günter Zöchbauer :
*ngIf="selectedElement && selectedElement['type']"