Created
November 18, 2022 19:38
-
-
Save pauleveritt/bfd3969f248cd0bb37d5f083f9d72887 to your computer and use it in GitHub Desktop.
Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Hello Animals</h1> | |
<ng-container *ngIf="isFish(pet)"> | |
<button (click)="pet.swim()">Swim!</button> | |
</ng-container> | |
<ng-container *ngIf="isFish(pet)"> | |
<button (click)="pet.fly()">Fly!</button> | |
</ng-container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, OnInit} from '@angular/core'; | |
abstract class Animal { | |
eats(): void { | |
} | |
} | |
class Fish extends Animal { | |
swims(): void { | |
} | |
} | |
class Bird extends Animal { | |
flys(): void { | |
} | |
} | |
@Component({ | |
selector: 'app-animal', | |
templateUrl: './animal.component.html', | |
styleUrls: ['./animal.component.css'] | |
}) | |
export class AnimalComponent implements OnInit { | |
pet: Animal = new Bird(); | |
constructor() { | |
} | |
ngOnInit(): void { | |
} | |
isFish(animal: Animal): animal is Fish { | |
return animal instanceof Fish; | |
} | |
isBird(animal: Animal): animal is Bird { | |
return animal instanceof Bird; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment