Currently diving into Angular and looking to create a method that can determine if an object is of type Dog
(identified by a woof
property).
This is the code snippet I have put together so far:
export class SomeClass {
public animal?: Dog | Cat;
...
public isDog() {
return 'woof' in this.animal; <-- Object may be 'undefined'
}
}
To display content based on the object's type, I am using this logic:
<div *ngIf="isDog(); then dogCardTemplate else catCardTemplate"></div>
The issue lies in the fact that this.animal
could potentially be undefined, resulting in the error message:
Object may be 'undefined'.
If anyone could provide a concise solution with minimal code modifications, it would be greatly appreciated.